我有以下代码,它是基于 msdn网站上的Monitor类示例。
这段代码在调用Monitor.Exit()时抛出SynchronizationLockException,并显示错误信息:"Object synchronization method was called from an unsynchronized block of code." 这个错误的解释是我试图释放一个我没有拥有的互斥锁,但是除非TryEnter成功,否则我无法进入调用Exit的代码块。如何消除这个错误?
private void WebRefresh_Click(object sender, EventArgs e)
{
if (WebRefresh.Enabled)//Only call from button
{
if (System.Threading.Monitor.TryEnter(deployIsRunning))
{
refreshWebVersion();
System.Threading.Monitor.Exit(deployIsRunning);
}
}
else
{
MessageBox.Show("You cannot refresh during a deploy");
}
}
这段代码在调用Monitor.Exit()时抛出SynchronizationLockException,并显示错误信息:"Object synchronization method was called from an unsynchronized block of code." 这个错误的解释是我试图释放一个我没有拥有的互斥锁,但是除非TryEnter成功,否则我无法进入调用Exit的代码块。如何消除这个错误?