using System;
using System.Threading;
namespace ReleaseHelper.Utils
{
public static class MutexExtensions
{
public delegate void DeleWriteLog(string txt);
public static DeleWriteLog WriteLog;
///
/// 进程间互斥操作
///
///
///
///
private static void Exec(string key, Action action, bool recursive)
{
using (Mutex mutex = new Mutex(initiallyOwned: false, name: key))
{
try
{
mutex.WaitOne();
action();
}
catch (AbandonedMutexException ex)
{
if (recursive)
{
WriteLog?.Invoke(ex.Message);
}
else
{
Exec(key, action, true);
}
}
finally
{
mutex.ReleaseMutex();
}
}
}
///
/// 进程间互斥操作
///
///
///
public static void Exec(string key, Action action)
{
Exec(key, action, false);
}
}
}