mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
Update SafeMutex.cs to implement IDisposable
This commit is contained in:
committed by
Drew Tingen
parent
6877b5c800
commit
be94503e6a
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp;
|
||||
#else
|
||||
@@ -12,8 +12,10 @@ namespace ICD.Common.Utils
|
||||
/// done with it. This class is an attempt to gracefully handle the ObjectDisposedExceptions
|
||||
/// we see on program termination, ocassionally causing the program to restart instead of stop.
|
||||
/// </summary>
|
||||
public sealed class SafeMutex
|
||||
public sealed class SafeMutex : IDisposable
|
||||
{
|
||||
private bool _disposedValue;
|
||||
|
||||
#if SIMPLSHARP
|
||||
private readonly CMutex m_Mutex;
|
||||
#else
|
||||
@@ -21,8 +23,8 @@ namespace ICD.Common.Utils
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// Initializes a new instance of the <see cref="SafeMutex"/> class.
|
||||
/// </summary>
|
||||
public SafeMutex()
|
||||
{
|
||||
#if SIMPLSHARP
|
||||
@@ -31,7 +33,20 @@ namespace ICD.Common.Utils
|
||||
m_Mutex = new Mutex();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SafeMutex"/> class.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="System.Threading.Mutex.Mutex(bool)"/>
|
||||
public SafeMutex(bool initiallyOwned)
|
||||
{
|
||||
#if SIMPLSHARP
|
||||
m_Mutex = new CMutex(initiallyOwned);
|
||||
#else
|
||||
m_Mutex = new Mutex(initiallyOwned);
|
||||
#endif
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
@@ -46,7 +61,7 @@ namespace ICD.Common.Utils
|
||||
#if SIMPLSHARP
|
||||
return m_Mutex.WaitForMutex(timeout);
|
||||
#else
|
||||
return m_Mutex.WaitOne(timeout);
|
||||
return m_Mutex.WaitOne(timeout);
|
||||
#endif
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
@@ -71,5 +86,50 @@ namespace ICD.Common.Utils
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region IDisposable
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
// dispose managed state (managed objects)
|
||||
try
|
||||
{
|
||||
// disposing of a mutex automatically releases it. Match that behavior
|
||||
m_mutex.ReleaseMutex();
|
||||
m_Mutex.Dispose();
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// Releasing a disposed mutex in this case is valid behaviour
|
||||
}
|
||||
}
|
||||
|
||||
// free unmanaged resources (unmanaged objects) and override finalizer
|
||||
// set large fields to null
|
||||
_disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
||||
// ~CaMutex()
|
||||
// {
|
||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
// Dispose(disposing: false);
|
||||
// }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user