473,796 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BackgroundWorke r: 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!

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

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

Similar topics

11
3278
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 "resources" I use are memory and file I/O and whenever there is a memory allocation failure or file I/O failure I just simply call a custom assert-type function to check, print a error message and abort. This seems to be OK for now (for the...
7
2342
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 of code, when System.Exception seems to get the job done for me. My application is an ASP.Net intranet site. When I catch an exception, I log the stack trace and deal with it, normally by displaying an error
3
12113
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 raise an exception and display a messagebox. however no matter where i put the Try block to trap the error, it does not work.
5
14134
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 my FileIO class) I've established Public Event in my core logic classes along with RaiseEvents (that will updated a progress bar on the UI side). This all works great when I'm NOT using Threading (BackgroundWorker), however, as soon as I...
1
2908
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 thread. Now I have in the backgroundworker's DoWork method a try catch finally block, like this: StreamReader sr = null;
14
6396
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 the do work method, it stops for a long while and then executes the first line of the method and the doesn't do anything else. What am i doing wrong?
0
2482
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 exception when trying to access BackgroundWorker.CancellationPending or e.Cancel An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll Additional information: Exception has been thrown by the target of...
2
4840
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 mscorlib.dll Additional information: Exception has been thrown by the target of an invocation. I am using the backgroundWorker in a component that I am trying to make for doing encryption of large files in the background.
13
2426
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 TreeView control I have on my main form. When I try to do that, I get the error "Cross-thread operation not valid: Control 'TreeView1' accessed from a thread other than the thread it was created on" I seem to have read something about that...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10465
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.