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
|
#if SIMPLSHARP
|
||||||
using Crestron.SimplSharp;
|
using Crestron.SimplSharp;
|
||||||
#else
|
#else
|
||||||
@@ -12,8 +12,10 @@ namespace ICD.Common.Utils
|
|||||||
/// done with it. This class is an attempt to gracefully handle the ObjectDisposedExceptions
|
/// 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.
|
/// we see on program termination, ocassionally causing the program to restart instead of stop.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class SafeMutex
|
public sealed class SafeMutex : IDisposable
|
||||||
{
|
{
|
||||||
|
private bool _disposedValue;
|
||||||
|
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
private readonly CMutex m_Mutex;
|
private readonly CMutex m_Mutex;
|
||||||
#else
|
#else
|
||||||
@@ -21,8 +23,8 @@ namespace ICD.Common.Utils
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Constructor.
|
/// Initializes a new instance of the <see cref="SafeMutex"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SafeMutex()
|
public SafeMutex()
|
||||||
{
|
{
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
@@ -31,7 +33,20 @@ namespace ICD.Common.Utils
|
|||||||
m_Mutex = new Mutex();
|
m_Mutex = new Mutex();
|
||||||
#endif
|
#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
|
#region Methods
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -46,7 +61,7 @@ namespace ICD.Common.Utils
|
|||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
return m_Mutex.WaitForMutex(timeout);
|
return m_Mutex.WaitForMutex(timeout);
|
||||||
#else
|
#else
|
||||||
return m_Mutex.WaitOne(timeout);
|
return m_Mutex.WaitOne(timeout);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
catch (ObjectDisposedException)
|
catch (ObjectDisposedException)
|
||||||
@@ -71,5 +86,50 @@ namespace ICD.Common.Utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#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