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

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

问题描述


我有一个带有 2 个组合框的表单.我想根据 combobox1.Textcombobox2.Text 填充 combobox2.DataSource (我假设用户已经在 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 );
}

至于构建 DataSource 是一个耗时的过程(它创建一个对数据库的请求并执行它),我决定最好使用 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).您可能需要将您的实际工作转移到另一个异步委托(或者更好的是,将工作提交到 ThreadPool),并让您的主工作线程轮询,直到该内部工作线程触发等待状态, 或者它检测到 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.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync.aspx

http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx

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

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