new mutex wrapper wiht autorelease
This commit is contained in:
parent
c1ac878fc8
commit
ef6a55b385
29
SafeMutex.cs
Normal file
29
SafeMutex.cs
Normal file
@ -0,0 +1,29 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user