如何将一段代码添加到由click触发的backgroundWorker?

时间:2013-05-13 12:51:18

标签: c# winforms click backgroundworker

我需要使用大量的file.copy,这使得我的form1"没有响应"并且我的程序显示了DeadLock异常,因此我想创建一个backgroundWorker来处理所有主要处理。 我做了什么:

按钮:

if (backgroundWorker1.IsBusy != true)
            {
                backgroundWorker1.RunWorkerAsync();
            }

DoWork的:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        bool continueWork = true;

        while (continueWork)
        {
            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
                break;
            }
            else
            {
                foreach (string name in listFiles) //global list
                {
                    string destwithFilename= dest + "\\" + Path.GetFileName(name);
                    try
                    { File.Copy(name, destwithFilename, false);}
                    catch (Exception EX_NAME)
                    {
                        Console.WriteLine(EX_NAME);
                    }
                    worker.ReportProgress((1));
                }
                pbStatus.Increment(50); //Error, I can't access form1, another thread.
                continueWork = false; //If job is done, break;
                System.Threading.Thread.Sleep(500);
            }
        }
    }

问题:

1)Form1仍然显示为"没有响应&#34 ;;

2)无法访问Form1;

3)即使使用backgroundWorker,仍会出现DeadLock异常。 //也许我应该禁用托管调试助手

修改

DoWork的

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        bool continueWork = true;

        while (continueWork)
        {
            foreach (string name in Files) //Global
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    string destwithFilename= dest + "\\" + Path.GetFileName(name);
                    try
                    {
                        File.Copy(name, destwithFilename, false); //no overwritting
                        worker.ReportProgress((1));
                        //System.Threading.Thread.Sleep(50);
                    }
                    catch (Exception EX_NAME)
                    {
                        Console.WriteLine(EX_NAME);
                    }
                }
            }
            continueWork = false;
        }
    }

ProgressChanged:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        pbProcess.Value = e.ProgressPercentage;
        if (pbProcess.Value == pbProcess.Maximum)
        {
            cbFinal.Checked = true;
        }
    }

结果:

输出真的很慢,但现在我的表单继续工作,没有"没有响应"。 pbProcess没有增加,我不知道为什么。 pbProcess是一个progressBar。

1 个答案:

答案 0 :(得分:1)

要报告进展情况,您应该:

  • WorkerReportsProgress属性设置为True
  • 使用背景工作者的ReportProgress()方法报告进度
  • 然后,处理后台工作人员的ProgressChanged事件。并设置pbStatus的值

代码:

报告进展情况

backgroundWorker1.ReportProgress(50);

处理ProgressChanged事件

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
      pbStatus.Value = e.ProgressPercentage;
}

<强>进展

对于进展,您使用ReportProgress(1)报告进度,这会将进度条的值设置为1,并且不会将其递增1

int cpt = 1;
int totalFilesCount = listFiles.Count;

foreach (var field in listFiles)
{
      // Copy the file ...

      backgroundWorker1.ReportProgress((cpt / totalFilesCount) * 100);
      cpt++;
}