DTLib/SafeMutex.cs
2021-09-29 18:02:27 +03:00

36 lines
842 B
C#

using System;
using System.Threading;
namespace DTLib
{
public class SafeMutex
{
readonly Mutex Mutex = new();
bool isReleased = false;
// тут выполняется отправленный код
public void Execute(Action action, out Exception exception)
{
try
{
exception = null;
Mutex.WaitOne();
action();
Mutex.ReleaseMutex();
isReleased = true;
}
catch (Exception ex)
{
exception = ex;
if (!isReleased) Mutex.ReleaseMutex();
}
}
public void Execute(Action action)
{
Execute(action, out Exception exception);
exception?.Throw();
}
}
}