BackgroundWorker: Do_Work, and Exception catching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maxwell

    BackgroundWorker: Do_Work, and Exception catching

    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!



  • Peter Duniho

    #2
    Re: BackgroundWorke r: Do_Work, and Exception catching

    On Wed, 30 Jul 2008 12:19:01 -0700, Maxwell
    <maxwell@discus sions.microsoft .comwrote:
    [...]
    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?
    AFAIK, RunWorkerComple tedEventArgs is for BackgroundWorke r's use,
    describing whether _BackgroundWork er_ was successful or not. It's not
    actually related to your own code. If you want to propogate an error
    back, you need to include it in your own data structures explicitly.

    At least, that's how I understand it. I could be wrong. :)

    Pete

    Comment

    Working...