Game Development Reference
In-Depth Information
Do not make excessive calls to the ReportProgress method, because each call adds additional over-
head to your background processing, taking longer to complete; however, it is also important to enable
users to witness the current progress of the tasks, making it tricky to find the right balance of use.
The PercentageProgress property of the ProgressChanged event arguments will
return the percentage completed value, set by the ReportProgress method. You can
also access the user state within the ProgressChanged event handler arguments. The
following code shows the implementation details for the progress changed event.
private void backgroundWorker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
OperationProgressBar.Value = e.ProgressPercentage;
ResultLabel.Text = String.Format(“Calculating: {0}%”,
BackgroundWorker events are not marshaled across AppDomain boundaries; therefore
you must not use BackgroundWorker to process tasks in more than one AppDomain .
You must be careful not to manipulate the user interface in your DoWork event
handler. The proper way is to communicate with the user interface through the
ProgressChanged and RunWorkerCompleted events.
Supporting User Cancellation
Allowing the user to cancel the progressing task is extremely easy. Just invoke the
CancelAsync method on the BackgroundWorker instance and then handle the
CancellationPending property in the DoWork event appropriately.
private void CancellationButton_Click(object sender, EventArgs e)
CancellationButton.Enabled = false;
It is important to know that if a call to CancelAsync sets CancellationPending to true just
after the last invocation of the DoWork event, then the code will not have the opportunity to set
the DoWorkEventArgs.Cancel flag to true. This results in the Cancelled flag being set to false
in the RunWorkerCompleted event. This problem occurs because of a race condition.
Search Nedrilad ::

Custom Search