Ian*_*Ian 5 c# multithreading manualresetevent
我对我正在使用的 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.
}
}
Run Code Online (Sandbox Code Playgroud)
Webclient 与您的调用者在同一线程中运行,因此该线程在 WaitOne 处被阻塞,它实际上并没有为您创建新线程。
将您的代码移至BackgroundWorker 中,或者简单地说,不要阻塞,而是等待引发DownloadComplete 事件。
| 归档时间: |
|
| 查看次数: |
12436 次 |
| 最近记录: |