我编写了一个使用工作线程的类,它使用事件对象。我把班级缩减到了基础:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace TEST
{
public class TEST_Worker
{
public static ManualResetEvent m_Event = new ManualResetEvent(false);
private BackgroundWorker m_backgroundWorker;
public TEST_Worker()
{
InitBackgroundWorker();
}
private void InitBackgroundWorker()
{
m_backgroundWorker = new BackgroundWorker();
m_backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
m_backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
m_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
m_backgroundWorker.WorkerReportsProgress = true;
}
public void start()
{
m_Event.Reset();
m_backgroundWorker.RunWorkerAsync();
}
public void stop()
{
m_backgroundWorker.CancelAsync();
}
public void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
m_backgroundWorker.ReportProgress(100, "Progress {0}%");
}
// This event handler updates the UI
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
m_Event.Set();
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// dispose managed resources
m_backgroundWorker.Dispose();
}
// free native resources
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
在表单类(菜单项处理程序)中,我这样做:
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
TEST.TEST_Worker myTest = new TEST.TEST_Worker();
myTest.start();
TEST.TEST_Worker.m_Event.WaitOne();
MessageBox.Show("Complete!");
}
我说得对。表单不需要睡眠或任何东西(否则GUI不会更新 - 真正的代码会进行条形更新)。但是,我还希望表单知道线程何时完成然后执行任务。目前它只是在致电start
之后继续。
显然,我没有以正确的方式使用ManualResetEvent
。目前,我的应用程序无限期挂起。它从不显示消息框(我不希望线程显示它 - 但是表单)。
这有意义吗?我是以错误的方式解决这个问题吗?
我之前使用过它,但不可否认的是,当一个工作线程需要等待子工作线程完成才能继续使用它。这个上下文是不同的,一个表单调用一个工作线程,然后想要在线程完成时做某事(但不要阻止表单进行GUI更新)。
答案 0 :(得分:3)
如评论中所述,您可能想尝试从包装后台工作者的类中提升自定义事件:
public class TEST_Worker
{
public event EventHandler OnCompleted = delegate { };
// ....
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
OnCompleted(this, EventArgs.Empty);
}
}
// In form:
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
TEST.TEST_Worker myTest = new TEST.TEST_Worker();
myTest.OnCompleted += (_sender, _e) => {
MessageBox.Show("Complete!");
};
myTest.start();
}
另一种方法是通过Invoke使用原始线程和同步访问表单,如下所述:Accessing a form's control from a separate thread