我有两种类似的方法,在MainThreadDoWork
方法中,循环完成执行,不管OtherThreadWork
方法中的autoResetEvent.Set()如何。任何想法在这个AutoResetEvent实例中发生了什么?AutoResetEvent发出信号之前触发
AutoResetEvent autoResetEvent = new AutoResetEvent(true);
private int count = 10;
private void MainThreadDoWork(object sender, EventArgs e)
{
for (int i = 0; i < count; i++)
{
if (autoResetEvent.WaitOne())
{
Console.WriteLine(i.ToString());
}
}
}
private void OtherThreadWork()
{
autoResetEvent.Set();
//DoSomething();
}
编辑
下面是OtherThreadWork如何实际样子。
private void OtherThreadWork()
{
if (textbox.InvokeRequired)
{
this.textbox.BeginInvoke(new MethodInvoker(delegate() { OtherThreadWork(); }));
autoResetEvent.Set();
}
else
{
// Some other code
}
}
东西是腥这里,我怀疑实际的代码有一些其他的问题。我在我的机器上运行了这段代码,并阻止了对'WaitOne'的调用。 – Tudor 2012-02-21 11:19:54
@Tudor:我发布了实际的otherThreadWork方法。有什么想法发生了什么?谢谢! – ABCD 2012-02-21 12:25:05
'MainThreadDoWork'正在UI线程上执行? – Tudor 2012-02-21 12:43:48