473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 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!

Jul 30 '08 #1
1 1843
On Wed, 30 Jul 2008 12:19:01 -0700, Maxwell
<ma*****@discussions.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, 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
Jul 31 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
3
by: chris | last post by:
hello, I can't seem to make this work: VS2005 I have a simple program that uses a backgroundworker control to execute a long process (webservice call) if that webservice call fails, i want to...
5
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
1
by: ditnooitlezen | last post by:
Hi, the (.NET 2.0) backgroundworker object has a DoWork method that operates in a background thread. When the DoWork method is finished the RunWorkerCompleted event is raised in the parent...
14
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to...
0
by: Chris | last post by:
I would like to be able to pass the BackgroundWorker object and DoWork Event Args to a second function (third function?) and be able to still report the progress. I'm getting the following...
2
by: Chris | last post by:
When I try to access the backgroundWorker.CancellationPending property I get the following exception: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in...
13
by: Johnny Jörgensen | last post by:
Hi I have a procudure that takes some time and thus slows down the main system. I want to put it in a backgroundworker component to run it asynchroneously. But in the procedure, I want to update a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.