由于出现被放弃的mutex,等待过程结束
下面程序运行过程中出现了个问题,请大家帮忙解决下!
static void Main(string[] args)
{
Program p = new Program();
p.RunThread();
Console.ReadLine();
}
public Program()
{
mutex = new Mutex();
thread1 = new Thread(new ThreadStart(thread1Func));
thread2 = new Thread(new ThreadStart(thread2Func));
}
public void RunThread()
{
thread1.Start();
thread2.Start();
}
private void thread1Func()
{
for (int count = 0; count < 3; count++)
{
mutex.WaitOne();
TestFunc("Thread1 have run " + count.ToString() + " times");
}
mutex.ReleaseMutex();
}
private void thread2Func()
{
for (int count = 0; count < 10; count++)
{
//下面waitone出出现“由于出现被放弃的mutex,等待过程结束”异常,我明明调用了mutex.ReleaseMutex()啊!
mutex.WaitOne();
TestFunc("Thread2 have run " + count.ToString() + " times");
mutex.ReleaseMutex();
}
}