Assigning Event Handlers to a BackgroundWorker

suggest change

Once the instance of the BackgroundWorker has been declared, it must be given properties and event handlers for the tasks it performs.

/* This is the backgroundworker's "DoWork" event handler. This 
   method is what will contain all the work you 
   wish to have your program perform without blocking the UI. */

bgWorker.DoWork += bgWorker_DoWork;

/*This is how the DoWork event method signature looks like:*/
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Work to be done here   
    // ...
    // To get a reference to the current Backgroundworker:
    BackgroundWorker worker = sender as BackgroundWorker;
    // The reference to the BackgroundWorker is often used to report progress
    worker.ReportProgress(...);
}

/*This is the method that will be run once the BackgroundWorker has completed its tasks */

bgWorker.RunWorkerCompleted += bgWorker_CompletedWork;

/*This is how the RunWorkerCompletedEvent event method signature looks like:*/
private void bgWorker_CompletedWork(object sender, RunWorkerCompletedEventArgs e)
{
    // Things to be done after the backgroundworker has finished
}

/* When you wish to have something occur when a change in progress 
 occurs, (like the completion of a specific task) the "ProgressChanged" 
 event handler is used. Note that ProgressChanged events may be invoked
 by calls to bgWorker.ReportProgress(...) only if bgWorker.WorkerReportsProgress
 is set to true.  */

 bgWorker.ProgressChanged += bgWorker_ProgressChanged;

/*This is how the ProgressChanged event method signature looks like:*/
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Things to be done when a progress change has been reported

    /* The ProgressChangedEventArgs gives access to a percentage,
     allowing for easy reporting of how far along a process is*/
    int progress = e.ProgressPercentage;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


BackgroundWorker:
* Assigning Event Handlers to a BackgroundWorker

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
113 BackgroundWorker
127 Caching
135 Pointers
147 C# Script