使用线程池时如何判断线程何时完成?
我的线程:
public void main_news_thread(MainApplication main)
{
ThreadPool.QueueUserWorkItem(p => check_news(validrsslist, 0));
}
我每隔一段时间就调用这个线程...
我如何知道线程何时完成,以便我可以调用处理 GUI 的其他两个方法?我如何引用这个线程池线程?
My thread:
public void main_news_thread(MainApplication main)
{
ThreadPool.QueueUserWorkItem(p => check_news(validrsslist, 0));
}
I call this thread every interval of time...
How can I know when the thread finishes so I can call two other methods which deal with the GUI? How can I refer to this threadpool thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您正在谈论 UI,因此您可能需要查看 BackgroundWorker,它提供了一个 RunWorkerCompleted 事件,该事件在 UI 线程上触发,并指示成功/失败/取消等。
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker_events.aspx
不过,就我个人而言,我只是在我的工作代码(记住通过 WPF 中的 Dispatcher.Invoke 或 winforms 中的 this.Invoke 切换回 UI 线程)。
Since you are talking about UI, you might want to look at BackgroundWorker, which offers a RunWorkerCompleted event that fires on the UI thread, and indicate success/failure/cancel etc.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker_events.aspx
Personally, though, I'd just run a callback method at the end of my worker code (remembering to switch back to the UI thread, via Dispatcher.Invoke in WPF or this.Invoke in winforms).
您可以在线程本身中执行方法(您必须注意调用自己来访问 gui 线程):
You can execute the methods in the thread itself (you have to take care of invoking yourself to access the gui thread):