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 );
}

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委托必须定期判断此属性并自行处理取消.

棘手的部分是,您的DoWork委托可能正在阻塞,这意味着您在数据源上所做的工作必须在您可以执行任何其他操作(如判断取消挂起)之前完成.您可能需要将实际工作转移到另一个异步委托(或者更好的做法是,将工作提交给ThreadPool),并让主工作线程轮询,直到该内部工作线程触发等待状态,或者检测到CancelationPending.

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

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

.net相关问答推荐

升级到.NET8后,SignalR(在坞站容器上)网关损坏

避免函数和其他对象之间的相互递归的模式?

从 Contentful 中的富文本元素中获取价值?

IIS 发布 ASP.NET Core 应用程序而不关闭 IIS 网站

查找所有源硬编码字符串

无法加载文件或程序集 Microsoft.Extensions.DependencyInjection.Abstractions,版本 = 1.1.0.0

为什么这个多态 C# 代码会打印它的功能?

NonSerialized 属性

如何在 C# 中创建表达式树来表示String.Contains("term")?

为 XML 编码文本数据的最佳方法

发布版本中的 Debug.WriteLine

为什么需要 XmlNamespaceManager?

String 是原始类型吗?

使用 .NET 中的代码更改桌面墙纸

C# 方法可以定义为采用的最大参数数是多少?

如何在不丢失数据的情况下重命名 Entity Framework 5 Code First 迁移中的数据库列?

在安全处理异常时避免首次机会异常消息

如何在安装后立即启动 .NET Windows 服务?

MVC4 HTTP 错误 403.14 - 禁止

当它被抛出和捕获时,不要在那个异常处停止调试器