Thursday, September 6, 2018

the BackgroundWorker in a WinForms app!

Make one like so:

private System.ComponentModel.BackgroundWorker maid
this.maid = new System.ComponentModel.BackgroundWorker();
this.maid.WorkerReportsProgress = true;

 
 

There is the concept of reporting progress like this:

maid.ReportProgress(20);

 
 

I don't understand it yet. There is a progress bar that runs in the application I am immediately looking at. What is above would set the progress bar to 20 percent. I guess the ReportProgress kicks off an event for the reporting. You may have events assigned like so:

this.maid.DoWork += new
      System.ComponentModel.DoWorkEventHandler(this.maid_DoWork);
this.maid.ProgressChanged += new
      System.ComponentModel.
      ProgressChangedEventHandler(this.maid_ProgressChanged);
this.maid.RunWorkerCompleted += new
      System.ComponentModel.
      RunWorkerCompletedEventHandler (this.maid_RunWorkerCompleted);

 
 

The methods for the three events above would start out like so:

  • private void maid_DoWork(object sender,
          DoWorkEventArgs e)
    {
  • private void maid_ProgressChanged(object sender,
          ProgressChangedEventArgs e)
    {
  • private void maid_RunWorkerCompleted(object sender,
          RunWorkerCompletedEventArgs e)
    {

 
 

You could call the first event like so:

object[] stuff = new object[4];
stuff[0] = 13;
stuff[1] = 42;
stuff[2] = "housekeeping?";
stuff[3] = myConnectionString;
maid.RunWorkerAsync(stuff);

 
 

I don't see an example of the last event getting specifically called in the application I am looking at. Maybe this is an example of something that happens upon the tear down of deconstruction in advance of garbage collection.

No comments:

Post a Comment