如何正确停止的BackgroundWorker [英] How to stop BackgroundWorker correctly

查看:321
本文介绍了如何正确停止的BackgroundWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有2组合框上的形式。我想,以填补 combobox2.DataSource 根据 combobox1.Text combobox2.Text (我假设用户已经完成输入 ComboBox1的,是输入的 combobox2 )。所以,我有一个事件处理程序 combobox2 是这样的:


I have a form with 2 comboboxes on it. And I want to fill combobox2.DataSource based on combobox1.Text and combobox2.Text (I assume that the user has completed input in combobox1 and is in the middle of inputting in combobox2). So I have an event handler for combobox2 like this:

private void combobox2_TextChanged(object sender, EventArgs e)
{
    if (cmbDataSourceExtractor.IsBusy)
       cmbDataSourceExtractor.CancelAsync();

    var filledComboboxValues = new FilledComboboxValues{ V1 = combobox1.Text,
       V2 = combobox2.Text};
    cmbDataSourceExtractor.RunWorkerAsync(filledComboboxValues );
}

至于建设数据源是耗费时间的过程(它会创建一个请求数据库,并执行它),我决定,这是更好地使用BackgroundWorker的另一个过程中执行它。因此,有当cmbDataSourceExtractor尚未完成其工作,并在用户键入多了一个符号的场景。在这种情况下,我得到这条线异常
cmbDataSourceExtractor.RunWorkerAsync(filledComboboxValues​​); 有关BackgroundWorker的正忙,不能在同一时间执行多个操作
如何摆脱这种异常?
在此先感谢!

As far as building DataSource is time-consuming process (it creates a request to database and executes it) I decided that it's better to perform it in another process using BackgroundWorker. So there's a scenario when cmbDataSourceExtractor hasn't completed its work and the user types one more symbol. In this case I get an exception on this line
cmbDataSourceExtractor.RunWorkerAsync(filledComboboxValues ); about that BackgroundWorker is busy and cannot perform several actions in the same time.
How to get rid of this exception?
Thanks in advance!

推荐答案

CancelAsync 实际上并没有中止你的线程或类似的东西。它发出了一个消息给工作线程的工作应当通过 BackgroundWorker.CancellationPending 取消。正在在后台运行你的DoWork的委托必须定期检查此属性,并办理注销本身。

CancelAsync doesn't actually abort your thread or anything like that. It sends a message to the worker thread that work should be cancelled via BackgroundWorker.CancellationPending. Your DoWork delegate that is being run in the background must periodically check this property and handle the cancellation itself.

最棘手的部分是你的DoWork委托可能是阻塞,这意味着你做你的数据源的工作必须完成,然后才能做任何事情(如检查CancellationPending)。您可能需要将您的实际工作中又一个异步委托(或还可能更好,提交作品的线程池),和你的主要工作线程民意调查,直到这种内在工作线程触发一个等待状态,或者它检测CancellationPending。

The tricky part is that your DoWork delegate is probably blocking, meaning that the work you do on your DataSource must complete before you can do anything else (like check for CancellationPending). You may need to move your actual work to yet another async delegate (or maybe better yet, submit the work to the ThreadPool), and have your main worker thread poll until this inner worker thread triggers a wait state, OR it detects CancellationPending.

<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync.aspx

HTTP://www.$c$cproject.com/KB/ CPP / BackgroundWorker_Threads.aspx

这篇关于如何正确停止的BackgroundWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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