作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我对我正在使用的 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.
}
}
最佳答案
Webclient 与您的调用者在同一个线程中运行,因此该线程在 WaitOne 处被阻塞,它实际上并没有为您创建一个新线程。
将您的代码移到 BackgroundWorker 中,或者简单地说,不要阻塞而是等待引发 DownloadComplete 事件。
关于c# - ManualResetEvent WaitOne 未解锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1205674/
我是一名优秀的程序员,十分优秀!