CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2018
    Posts
    12

    Cancel function does not work with backgroundWorker?

    Cancel function does not work this code below. Can someone tell me what is wrong with this code?
    Code:
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
    
    Array.ForEach(originalFiles, (originalFileLocation) =>
                    {
                        FileInfo originalFile = new FileInfo(originalFileLocation);
                        FileInfo destFile = new FileInfo(originalFileLocation.Replace(sourcePath, destPath));
                        if (backgroundWorker1.CancellationPending == true)
                        {
                            e.Cancel = true;
                            return;
                        }
    
                        if (destFile.Exists)
                        {
                           if (originalFile.Length != destFile.Length || originalFile.LastWriteTime != destFile.LastWriteTime)
                           {
                               originalFile.CopyTo(destFile.FullName, true);
                               count2++;
                               answer = count2 / count * 100;
                               answer = Math.Round(answer);
                               backgroundWorker1.ReportProgress(Decimal.ToInt32(answer));
                               textFile = "Copying file " + destFile.FullName.ToString();
                               logFile.Add(textFile);
                                countLines++;
                           }
                        }
                        else
                        {
                            Directory.CreateDirectory(destFile.DirectoryName);
                            originalFile.CopyTo(destFile.FullName, false);
                            count2++;
                            answer = count2 / count * 100;
                            answer = Math.Round(answer);
                            backgroundWorker1.ReportProgress(Decimal.ToInt32(answer));
                            textFile = "Copying file " + destFile.FullName.ToString();
                            logFile.Add(textFile);
                            countLines++;
                        }
                    });
    }
    
      private void buttonCancel_Click(object sender, EventArgs e)
            {
                backgroundWorker1.WorkerSupportsCancellation = true;
                backgroundWorker1.CancelAsync();
                buttonCancel.Enabled = false;
            }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cancel function does not work with backgroundWorker?

    Move backgroundWorker1.WorkerSupportsCancellation = true; to before where you start the thread, not inside the cancel handler.

    Also, the docs state not to reference the background worker object directly from inside the worker thread. Instead use the sender property and local variable.

    from the docs... (https://docs.microsoft.com/en-us/dot...orkdesktop-4.8)

    // Do not access the form's BackgroundWorker reference directly.
    // Instead, use the reference provided by the sender parameter.
    BackgroundWorker bw = sender as BackgroundWorker;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured