Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2008

Tip: Looking for answers? Try searching our database.

BackgroundWorker: Do_Work, and Exception catching

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Maxwell - 30 Jul 2008 20:19 GMT
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!
Peter Duniho - 31 Jul 2008 06:50 GMT
> [...]
> 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, RunWorkerCompletedEventArgs is for BackgroundWorker's use,  
describing whether _BackgroundWorker_ 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.