backgroundWorker取消后,重新开始就报错:此 BackgroundWorker 当前正忙,无法同时运行多个任务。

使用BackgroundWorker控件,有2个按钮buttonBegin和buttonCancel。
其他都正常,只是在用buttonBegin开始运行,然后点击buttonCancel取消后,到这里都正常。
可当再次点击buttonBegin时,就报错说:
“此 BackgroundWorker 当前正忙,无法同时运行多个任务。”查过资料,说是:this.backgroundWorker1.CancelAsync()只是将CancellationPending设置为true,并没有终止进程。
我现在想做的是,取消后,重新点击buttonBegin会重新运行。
感觉应该在buttonCancel_Click后面增加个终止该backgroundWorker1的方法,可查了半天也没找到。
代码如下:
 private void buttonBegin_Click(object sender, EventArgs e)
        {
            string[] pramlist = { "0"};
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.RunWorkerAsync(pramlist);
        }        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.CancelAsync();
            buttonCancel.Enabled = false;
        }        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            string[] pramlist = (string[])e.Argument;
            e.Result = DownLoad(worker, e, pramlist);
        }        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {    
            this.progressBar1.Value = e.ProgressPercentage;
            labelMsg.Text = string.Format("下载中{0}%,{1}", e.ProgressPercentage, e.UserState.ToString());
        }        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                labelMsg.Text = "下载被取消!";
            }
            else
            {
                this.progressBar1.Value = 0;
                labelMsg.Text = bool.Parse(e.Result.ToString()) ? "下载成功!" : "下载失败!";
            }
           buttonCancel.Enabled = false;
        }
        private bool DownLoad(BackgroundWorker worker, DoWorkEventArgs e, string[] pramlist)
        {
            for (int i = 0; i < 100; i++)
            {
                System.Threading.Thread.Sleep(1000);                worker.ReportProgress(i, "下载开始");
            }
          
            return true;
        }

 
 

解决方案 »

  1.  
  2.  

    别用控件,你直接用class,每次用之前new个新的对象即可。
      

     

  3.  

    CancelAsync之后,BackgroundWorker.CancellationPending = true
    你要自己处理,具体如下:private bool DownLoad(BackgroundWorker worker, DoWorkEventArgs e, string[] pramlist)
            {
                for (int i = 0; i < 100; i++)
                {
                     if(worker.CancellationPending)
                    {
                       return false;
                    }        
                    System.Threading.Thread.Sleep(1000);                worker.ReportProgress(i, "下载开始");
                }
              
                return true;
            }
    其次Begin加IsBusy判断
    private void buttonBegin_Click(object sender, EventArgs e)
            {
                 if(backgroundWorker1.IsBusy)
                {
                    return;
                }
                string[] pramlist = { "0"};            backgroundWorker1.WorkerReportsProgress = true;
                backgroundWorker1.WorkerSupportsCancellation = true;
                backgroundWorker1.RunWorkerAsync(pramlist);
            }
      

     

  4. 需要在DoWork判断是否已经取消了,如果取消了,需要返回 
    backgroundworker有IsBusy属性,直接检测
  5. 报错:此 BackgroundWorker 当前正忙,无法同时运行多个任务


    CancelAsync之后,BackgroundWorker.CancellationPending = true
    你要自己处理,具体如下:

    private bool DownLoad(BackgroundWorker worker, DoWorkEventArgs e, string[] pramlist)
            {
                for (int i = 0; i < 100; i++)
                {
                     if(worker.CancellationPending)
                    {
                       return false;
                    }        
                    System.Threading.Thread.Sleep(1000);
    
                    worker.ReportProgress(i, "下载开始");
                }
              
                return true;
            }
    


     


    其次Begin加IsBusy判断

    private void buttonBegin_Click(object sender, EventArgs e)
            {
                 if(backgroundWorker1.IsBusy)
                {
                    return;
                }
                string[] pramlist = { "0"};
    
                backgroundWorker1.WorkerReportsProgress = true;
                backgroundWorker1.WorkerSupportsCancellation = true;
                backgroundWorker1.RunWorkerAsync(pramlist);
            }

posted on 2018-10-22 15:04  asdyzh  阅读(1205)  评论(0编辑  收藏  举报

导航