我对我正在使用的 ManualResetEvent 有点困惑,它似乎没有解除阻塞。有谁知道为什么会这样?
我遇到的情况是这样的。实际情况非常复杂,我没有设法隔离一段可以合理发布以重现问题的代码。
编辑
我已经更新了下面的代码示例。这是在许多不同的对话框中执行的,我注意到其中一个点击了 this.mre.WaitOne(); 然后会发生一个“服务器忙”对话框,我需要在其中按“切换到”或“重试”,这将允许我的代码逐步通过 WaitOne() 调用,一切都会正常工作。我不确定它的相关性,但显然它很重要。
public class A
{
ManualResetEvent mre;
public void Start(ThreadClass tc)
{
this.mre = new ManualResetEvent(false);
tc.Begin();
WebClient wc = new WebClient();
// progress events are pumped to the ThreadClass which then update the Form2.
wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync("Src", "Tgt");
this.mre.WaitOne();
}
void void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
try
{
// Do Stuff
}
finally
{
this.mre.Set();
}
}
}
public class ThreadClass
{
Begin()
{
Thread t = new Thread(new ThreadStart(DoWork));
t.Start();
}
private void DoWork()
{
Form f = new Form2();
f.ShowDialog();
// Sits waiting on another ResetEvent to determine when to close the thread.
}
}