BackgroundWorker的VS后台线程 [英] BackgroundWorker vs background Thread

查看:147
本文介绍了BackgroundWorker的VS后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有后台线程执行我应该在Windows窗体应用程序中使用的选择一个风格问题。目前,我有一个的BackgroundWorker 具有无限(而(真))循环窗体上。在这个循环中我使用 WaitHandle.WaitAny 保持螺纹打盹,直到感兴趣的东西发生。一个事件的处理,我等待的是一个 StopThread 事件,这样我可以打破循环。此事件时,从我的信号覆盖 Form.Dispose()

I have a stylistic question about the choice of background thread implementation I should use on a windows form app. Currently I have a BackgroundWorker on a form that has an infinite (while(true)) loop. In this loop I use WaitHandle.WaitAny to keep the thread snoozing until something of interest happens. One of the event handles I wait on is a "StopThread" event so that I can break out of the loop. This event is signaled when from my overridden Form.Dispose().

我读的地方,的BackgroundWorker 真的适用于你不想占用用户界面并掌握了有限的结束操作 - 如下载文件,或者处理项目的顺序。在这种情况下,端是未知的,仅当窗口关闭。因此,会是比较适合我使用一个后台线程,而不是的BackgroundWorker 为了这个目的?

I read somewhere that BackgroundWorker is really intended for operations that you don't want to tie up the UI with and have an finite end - like downloading a file, or processing a sequence of items. In this case the "end" is unknown and only when the window is closed. Therefore would it be more appropriate for me to use a background Thread instead of BackgroundWorker for this purpose?

推荐答案

这是我对你的问题的了解,您使用的是的BackgroundWorker 作为一个标准的线程。

From my understanding of your question, you are using a BackgroundWorker as a standard Thread.

该干嘛的BackgroundWorker 建议的事情,你不想占用UI线程的原因是因为它做的赢窗体开发时暴露了一些不错的活动。

The reason why BackgroundWorker is recommended for things that you don't want to tie up the UI thread is because it exposes some nice events when doing Win Forms development.

喜欢 RunWorkerCompleted 活动信号当线程完成它需要做的,而 ProgressChanged 事件更新的螺纹进展GUI中。

Events like RunWorkerCompleted to signal when the thread has completed what it needed to do, and the ProgressChanged event to update the GUI on the threads progress.

所以,如果你的不是的利用这些,我看不出在使用标准螺纹为你需要做任何伤害。

So if you aren't making use of these, I don't see any harm in using a standard Thread for what you need to do.

这篇关于BackgroundWorker的VS后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆