本文介绍了这BackgroundWorker的正忙,不能同时运行多个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我点击一个按钮,两次启动的BackgroundWorker我得到这个错误。
I get this error if I click a button that starts the backgroundworker twice.
This BackgroundWorker is currently busy and cannot run multiple tasks concurrently
我怎样才能避免这种情况?
How can I avoid this?
推荐答案
简单:不要启动的BackgroundWorker两次
Simple: Don't start the BackgroundWorker twice.
您可以检查它是否已被使用运行 IsBusy
属性,因此只需更改此代码:
You can check if it is already running by using the IsBusy
property, so just change this code:
worker.RunWorkerAsync();
这样:
to this:
if( !worker.IsBusy )
worker.RunWorkerAsync();
else
MessageBox.Show("Can't run the worker twice!");
更新:
Update:
如果你这样做实际上需要在同一时间启动多个后台任务,你可以简单地创建多个BackgroundWorker的对象
If you do actually need to launch multiple background tasks at the same time, you can simply create multiple BackgroundWorker objects
这篇关于这BackgroundWorker的正忙,不能同时运行多个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!