我正在使用一个命名互斥锁来锁定对文件(路径为 'strFilePath')的访问,构造方式如下:
private void DoSomethingsWithAFile(string strFilePath)
{
Mutex mutex = new Mutex(false,strFilePath.Replace("\\",""));
try
{
mutex.WaitOne();
//do something with the file....
}
catch(Exception ex)
{
//handle exception
}
finally
{
mutex.ReleaseMutex();
}
}
因此,这种方法只有在处理相同文件时才会阻塞线程。我测试了一下,似乎可以正常工作,但我真的很想知道你对此的想法。