using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SHJX.Service.PortClient
{
///
/// 任务互斥锁工具类
///
public class TaskMutexUtil : IDisposable
{
///
/// 定义一个互斥锁的实例
///
private readonly Mutex mut;
///
/// 判断是否已有相同名称的互斥锁存在
/// false:已存在,true:未存在
///
public bool ResBool { get; set; }
/// 互斥锁名称
public TaskMutexUtil(string mutexKey)
{
mut = new Mutex(false, mutexKey, out bool res);
ResBool = res;
mut.WaitOne();
}
///
/// 释放
///
public void Dispose()
{
//释放锁,让其他进程(或线程)得以继续执行
mut.ReleaseMutex();
//释放当前锁资源,为了避免互斥体占用唯一名称导致后续无法判断
mut.Dispose();
}
}
}