C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Second: Highlight backgroundWorker1. Click on the backgroundWorker1 item in the gray bar on the bottom. Now, look at the Properties panel.
Third: Look for the lightning bolt. You will see a lightning bolt icon in the Properties pane. This is Microsoft's icon for events.
Tip: BackgroundWorker is event-driven, so this is where we will make the necessary events to use it.
Tip: This UI is far better than trying to type the methods in manually. It requires less effort on your part.
Sleep: The Thread.Sleep method will pause the execution of the BackgroundWorker. It will show us threading is functioning.
SleepExample that shows DoWork event handler: C#
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(1000); // One second.
}
}
}
BackgroundWorker property list
DoWorkEventArgs e
Contains e.Argument and e.Result.
It is used to access those properties.
e.Argument
Used to get the parameter reference received by RunWorkerAsync.
e.Result
Check to see what the BackgroundWorker processing did.
backgroundWorker1.RunWorkerAsync
Called to start a process on the worker thread.
Tip: Changing variables from multiple threads at once often leads to bugs. A variable can become invalid.
TestObject: An example argument is created. You will have something more important and complex in your program.
RunWorkerAsync: This can be called anywhere in your code. We use a constructor, but that isn't important.
Example that shows RunWorkerAsync call: C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//
// Example argument object
//
TestObject test = new TestObject
{
OneValue = 5,
TwoValue = 4
};
//
// Send argument to our worker thread
//
backgroundWorker1.RunWorkerAsync(test);
}
}
/// <summary>
/// The test class for our example.
/// </summary>
class TestObject
{
public int OneValue { get; set; }
public int TwoValue { get; set; }
}
Note: The TestObject was passed to RunWorkerAsync, and that is received as e.Argument. We also have to cast.
Example that implements DoWork: C#
/// <summary>
/// Where we do the work in the program (the expensive slow stuff).
/// </summary>
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//
// e.Argument always contains whatever was sent to the background worker
// in RunWorkerAsync. We can simply cast it to its original type.
//
TestObject argumentTest = e.Argument as TestObject;
//
// Boring....
//
Thread.Sleep(10000);
argumentTest.OneValue = 6;
argumentTest.TwoValue = 3;
//
// Now, return the values we generated in this method.
// ... Use e.Result.
//
e.Result = argumentTest;
}
Next: You will get some autogenerated code that looks just like this. Put the argument receiving code in this method.
Example that shows RunWorkerCompleted event handler: C#
/// <summary>
/// This is on the main thread, so we can update a TextBox or anything.
/// </summary>
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
//
// Receive the result from DoWork, and display it.
//
TestObject test = e.Result as TestObject;
this.Text = test.OneValue.ToString() + " " + test.TwoValue.ToString();
//
// Will display "6 3" in title Text (in this example)
//
}
However: Once you understand that BackgroundWorker is just a structural overlay to threads in Windows Forms, it is intuitive.
Second: Custom processing is run. Your expensive code is executed in the DoWork method. Insert pause here as your program works.
Third: It finishes. When your processing is done, RunWorkerCompleted is called. In this method, you receive the result.
Also: The WPF ProgressBar is covered. It too can be used with a BackgroundWorker control.
ProgressBar: WPF