Game Development Reference
Table 37.1 Important Members of BackgroundWorker
Invoking this method will cancel the progressing task.
Invoking CancelAsync will set this property to true,
signifying that the user has requested cancellation of the task.
This event is fired when RunWorkerAsync is invoked.
This event is fired when ReportProgress is invoked.
Invoking this method will fire the ProgressChanged event,
updating the progress of the operation.
Executes the task asynchronously on a worker thread.
This event is fired when the task is completed or cancelled,
or when an unhandled exception is thrown within the
Boolean property that specifies whether or not to report
Boolean property that specifies whether or not the task can
You can create BackgroundWorker programmatically or by dragging it onto your
form from the Components tab of the Visual Studio toolbox. The example in this
chapter shows programmatic instantiation and configuration. The following code
shows how to instantiate the BackgroundWorker .
backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.WorkerSupportsCancellation = true;
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler
The DoWork event provides an instance of DoWorkEventArgs as a parameter, which
handles the input, output, and cancellation properties of the worker thread. Table
37.2 shows the properties of DoWorkEventArgs .