Question

The Background worker in my app does not report progress. (A breakpoint is set, but it is never reached). The code I'm using is attached below. I've been working on this for a couple hours and I'm stumped. The 'usual suspects' (setting the boolean for WorkerReportsProgress, and Linking the event to _ProgressChanged, forgetting to call .RunWorkerAsyn() have been checked) I'm puzzled why my breakpoint is never reached.

public partial class Form1 : Form
{
    BackgroundWorker bw_copyToTemp;  //Defined as a member of the class

    private void btnCopyToTemp_Click(object sender, EventArgs e)
    {

       bw_copyToTemp = new System.ComponentModel.BackgroundWorker
       {
           WorkerReportsProgress = true,
           WorkerSupportsCancellation = true
       };
       bw_copyToTemp.DoWork += bw_copyToTemp_DoWork;
       bw_copyToTemp.ProgressChanged += bw_copyToTemp_ProgressChanged;
       bw_copyToTemp.RunWorkerCompleted += bw_copyToTemp_RunWorkerCompleted;


       // Start background worker
       bw_copyToTemp.RunWorkerAsync();
    }

    void bw_copyToTemp_ProgressChanged(object sender,System.ComponentModel.ProgressChangedEventArgs e)
    {
        CurrentState s = (CurrentState)e.UserState;  //<<BREAKPOINT HERE NOT HIT
        toolStripStatusLabel1.Text = s.ProcessStep;
        toolStripProgressBar1.Value = e.ProgressPercentage;
    }

    void bw_copyToTemp_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {

        Properties.Settings prop = Properties.Settings.Default;
        BackgroundWorker worker = sender as BackgroundWorker;
        if (worker.WorkerReportsProgress)
            // Initialize the object that the background worker calls.
            worker.ReportProgress(0, new CurrentState("Called from within form"));  //<<< THIS LINE IS PROCESSED SINGLE STEPPING THROUGH CODE (NO CHANGE IN GUI)

        copyFileOnDVDToTemp.CopyOpticalToHardDrive(worker, e);


    }

    void bw_copyToTemp_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
        // test code goes here
    }
}

The "Current state" class is in a different .cs file with the same namespace:

   public class CurrentState
        {
            public CurrentState() { }
            public CurrentState(string processStep)
            {
                this.ProcessStep = processStep;
            }

            public string ProcessStep { get; set; }

        }
Was it helpful?

Solution

You have missed to call BackgroundWorker.RunWorkerAsync() method in the Click event handler of the button. Your worker does not get started after initialization.

private void btnCopyToTemp_Click(object sender, EventArgs e)
{

   bw_copyToTemp = new System.ComponentModel.BackgroundWorker
   {
       WorkerReportsProgress = true,
       WorkerSupportsCancellation = true
   };
   bw_copyToTemp.DoWork += bw_copyToTemp_DoWork;
   bw_copyToTemp.ProgressChanged += bw_copyToTemp_ProgressChanged;
   bw_copyToTemp.RunWorkerCompleted += bw_copyToTemp_RunWorkerCompleted;

   // Start background worker
   bw_copyToTemp.RunWorkerAsync();
}

Try the following to see that ReportProgress is called while DoWork is running:

    void bw_copyToTemp_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
    {
        // Set breakpoint here
        CurrentState s = (CurrentState)e.UserState; 
    }

    void bw_copyToTemp_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 0; i <= 100; i++)
        {
            // Set breakpoint here
            worker.ReportProgress(i, new CurrentState("Percentage"));
        }
    }

Debug your application and see how the breakpoints get hit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top