我写了一小段代码。像下面的东西
public static void SetLicence1()
{
Console.WriteLine("Setting Aspose Licence in Thread1 ");
Console.WriteLine(SetAsposeLicense());
}
public static void SetLicence2()
{
Console.WriteLine("Setting Aspose Licence in Thread2 ");
Console.WriteLine(SetAsposeLicense());
}
public static bool SetAsposeLicense()
{
try
{
//Declare Mutex variable:
using (Mutex mutex = new System.Threading.Mutex(false, "Test"))
{
mutex.WaitOne(TimeSpan.FromSeconds(5));
var objLic = new License();
objLic.SetLicense(@"C:\Nivedita\License\Aspose.Cells.lic");
mutex.ReleaseMutex();
}
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
return false;
}
}
}
public class TestClass
{
public static void Main()
{
Thread tid1 = new Thread(new ThreadStart(ThreadClass.SetLicence1));
Thread tid2 = new Thread(new ThreadStart(ThreadClass.SetLicence2));
tid1.Start();
tid2.Start();
Console.Read();
}
}
这段代码运行良好。但在这里我的问题是,WaitOne() 方法是否有可能卡在进程中或跨进程而互斥对象没有被释放?虽然我使用了 mutex.ReleaseMutex()。