Game Development Reference
In-Depth Information
Table 37.2 Properties of DoWorkEventArgs
Property
Description
e.Argument
Defined as an object, so any arbitrary data type can be used as an input
argument for the DoWork event. This parameter is passed into the
RunWorkerAsync method.
e.Cancel
This property allows you to cancel the progressing task. Setting this property to
true will cancel the task and move the context to the RunWorkerCompleted
event with a cancelled status. This property is used in conjunction with the
CancellationPending property to determine whether or not the user has
issued a cancellation request.
e.Result
Defined as an object, so any arbitrary data type can be used as an output result
to the RunWorkerCompleted event. This property is a way to communicate the
result or status back to the user interface.
The following code shows the implementation behind the DoWork event for the
Fibonacci calculator. Notice the exception that is thrown when the compute num-
ber has an invalid value. This is because any values higher than 91 will result in an
overflow with the long data type.
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (((int)e.Argument < 0) || ((int)e.Argument > 91))
{
throw new ArgumentException(“Compute number must be >= 0 and <= 91”);
}
e.Result = ComputeFibonacci((int)e.Argument, (BackgroundWorker)sender, e);
}
The following code shows the actual processing logic behind the Fibonacci calcu-
lations. This logic is in its own method because it calculates the numbers using
recursion.
private long ComputeFibonacci(int computeNumber,
BackgroundWorker worker,
DoWorkEventArgs e)
{
long result = 0;
Search Nedrilad ::




Custom Search