如何正确停止BackgroundWorker

编程入门 行业动态 更新时间:2024-10-20 09:28:31
本文介绍了如何正确停止BackgroundWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有 2 个组合框的表单.我想根据 combobox1.Text 和 combobox2.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.

msdn.microsoft/en-us/library/systemponentmodel.backgroundworker.cancelasync.aspx

www.codeproject/KB/cpp/BackgroundWorker_Threads.aspx

更多推荐

如何正确停止BackgroundWorker

本文发布于:2023-11-26 00:25:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何正确   BackgroundWorker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!