This BackgroundWorker is currently busy and cannot run multiple tasks concurrently

前端 未结 4 934
野趣味
野趣味 2020-12-08 04:06

I get this error if I click a button that starts the backgroundworker twice.

This BackgroundWorker is currently busy and cannot run multiple tasks concurrent         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 04:31

    Simple: Don't start the BackgroundWorker twice.

    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:

    If you do actually need to launch multiple background tasks at the same time, you can simply create multiple BackgroundWorker objects

提交回复
热议问题