So, I am building a class which implements the Event-based Asynchronous
pattern. We'll call it MyClass. Suppose part of it looks like the following:
/// <code>
public class MyClass : Component
{
private BackgroundWorker worker;
public void MyMethodAsync()
{
worker.RunWorkerAsync();
}
void worker_DoWork( object sender, DoWorkerEventArgs e )
{
this.runningAsync = true;
this.busy = true;
e.Result = DoThreeThings();
}
void worker_RunWorkerCompleted( object sender,
RunWorkerCompletedEventArgs e )
{
this.busy = false;
this.runningAsync = false;
MyMethodCompletedEventArgs results =
new MyMethodCompletedEventArgs(
(int) e.Results,
e.Error,
this.cancelled,
new object() );
MyMethodCompleted( this, results );
}
public int DoThreeThings()
{
int result;
result = ThingOne();
if( result != NoError )
{
return result;
}
result = ThingTwo();
if( result != NoError )
{
return result;
}
result = ThingThree();
if( result != NoError )
{
return result;
}
}
int ThingOne() { return NoError; }
int ThingTwo() { throw new System.IO.FileNotFoundException();
return NoError; }
int ThingThree() { return NoError; }
}
/// </code>
And in the client, a Windows Form control, I have the following
MyMethodCompletedHandler:
/// <code>
public class MyForm : Form
{
void button1_Click( object sender, EventArgs e )
{
myClass1.MyMethodAsync();
}
void myClass1_MyMethodCompleted( object sender,
MyMethodCompletedEventArgs e )
{
if ( e.Error != null )
{
MessageBox.Show( "Exception caught in MyMethod: " +
e.Error.ToString() );
}
else
{
MessageBox.Show( "MyMethod completed successfully! "
+ "Result: " + e.Result );
}
}
}
/// </code>
So, I would think that exceptions are handled, and they would be in the
e.Error property. But I get an unhandled exception error. Where should I be
checking for this exception, and how does in get into the e.Error property?
Currently I am, in the constructor of the MyMethodCompletedEventArgs class,
overloading it to add an int result, and then passing the other parameters
to the base class, AsyncCompletedEventArgs.
Thanks for any help!