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 BackgroundWorke r worker;
public void MyMethodAsync()
{
worker.RunWorke rAsync();
}
void worker_DoWork( object sender, DoWorkerEventAr gs e )
{
this.runningAsy nc = true;
this.busy = true;
e.Result = DoThreeThings() ;
}
void worker_RunWorke rCompleted( object sender,
RunWorkerComple tedEventArgs e )
{
this.busy = false;
this.runningAsy nc = false;
MyMethodComplet edEventArgs results =
new MyMethodComplet edEventArgs(
(int) e.Results,
e.Error,
this.cancelled,
new object() );
MyMethodComplet ed( 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.FileN otFoundExceptio n();
return NoError; }
int ThingThree() { return NoError; }
}
/// </code>
And in the client, a Windows Form control, I have the following
MyMethodComplet edHandler:
/// <code>
public class MyForm : Form
{
void button1_Click( object sender, EventArgs e )
{
myClass1.MyMeth odAsync();
}
void myClass1_MyMeth odCompleted( object sender,
MyMethodComplet edEventArgs e )
{
if ( e.Error != null )
{
MessageBox.Show ( "Exception caught in MyMethod: " +
e.Error.ToStrin g() );
}
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 MyMethodComplet edEventArgs class,
overloading it to add an int result, and then passing the other parameters
to the base class, AsyncCompletedE ventArgs.
Thanks for any help!
pattern. We'll call it MyClass. Suppose part of it looks like the following:
/// <code>
public class MyClass : Component
{
private BackgroundWorke r worker;
public void MyMethodAsync()
{
worker.RunWorke rAsync();
}
void worker_DoWork( object sender, DoWorkerEventAr gs e )
{
this.runningAsy nc = true;
this.busy = true;
e.Result = DoThreeThings() ;
}
void worker_RunWorke rCompleted( object sender,
RunWorkerComple tedEventArgs e )
{
this.busy = false;
this.runningAsy nc = false;
MyMethodComplet edEventArgs results =
new MyMethodComplet edEventArgs(
(int) e.Results,
e.Error,
this.cancelled,
new object() );
MyMethodComplet ed( 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.FileN otFoundExceptio n();
return NoError; }
int ThingThree() { return NoError; }
}
/// </code>
And in the client, a Windows Form control, I have the following
MyMethodComplet edHandler:
/// <code>
public class MyForm : Form
{
void button1_Click( object sender, EventArgs e )
{
myClass1.MyMeth odAsync();
}
void myClass1_MyMeth odCompleted( object sender,
MyMethodComplet edEventArgs e )
{
if ( e.Error != null )
{
MessageBox.Show ( "Exception caught in MyMethod: " +
e.Error.ToStrin g() );
}
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 MyMethodComplet edEventArgs class,
overloading it to add an int result, and then passing the other parameters
to the base class, AsyncCompletedE ventArgs.
Thanks for any help!
Comment