0

I need to work with a lot of file.copy, this makes my form1 "not responding" and my program show DeadLock exception, so I want to create a backgroundWorker to handle all the main processing. What I did:

Button:

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

Problems:

1) Form1 still appear as "Not responding";

2) Form1 can't be accessed;

3) Even with a backgroundWorker, DeadLock exception still appears. //Maybe I should disable Managed Debug Assistants

EDIT

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

Result:

Output is really slow, but now my form continue the work without "not responding". pbProcess does not increment, I'm not sure why. pbProcess is a progressBar.

4

1 回答 1

1

要报告进展,您应该:

  • WorkerReportsProgress属性设置为 True
  • ReportProgress()使用backgroun worker的方法上报进度
  • 然后,处理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++;
}
于 2013-05-13T12:59:16.990 回答