【发布时间】:2011-01-21 14:04:12
【问题描述】:
我有以下代码,它基于Monitor class example on the msdn website。
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 并显示错误消息:“对象同步方法是从未同步的代码块中调用的。”错误的解释是我试图释放一个我不拥有的互斥锁,但我无法进入调用Exit 的代码块,除非TryEnter 成功。如何消除此错误?
【问题讨论】:
标签: c# multithreading