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

Home Posts Topics Members FAQ

Backgroundworke r handles exception wrongly

Hi,

the (.NET 2.0) backgroundworke r object has a DoWork method that
operates in a background thread. When the DoWork method is finished the
RunWorkerComple ted event is raised in the parent thread.

Now I have in the backgroundworke r's DoWork method a try catch finally
block, like this:

StreamReader sr = null;
try
{
sr = new StreamReader("b lahdieblah" +
e.Argument.ToSt ring());
if (!this.Cancella tionPending)
try
{
e.Result = sr.ReadToEnd();
}
catch (Exception exc)
{
throw exc;
}
else
{
e.Cancel = true;
e.Result = null;
}
}
catch (Exception exc)
{
throw exc;
}
finally
{
if (sr != null) sr.Close();
}
return;

The "blahdiebla h" part is to force an error. The 'e' is een event
argument that is passed to the DoWork method.
The error is catched, but code execution always breaks at the 'throw
exc' line.
I presumed that the error should have been propagated to the
RunWorkerComple tedEventArgs, in it's .Error property. However it never
gets there.

What did I miss? When and how will the .Error property be filled and
RunWorkerComple ted called, like the documentation states.

Sep 5 '06 #1
1 2908
I hereby add the following new information to the question:
- it appears that by removing the 'catch' block, the exceptionis
raised to the parent thread (shouldn't 'throw exc' have done the
same?)
- the application then generates a 'first chance exception' (green bar
break - only when in debug mode)

Question 2: is it perhaps a better practice to propagate the exception
"manually" in the Result property of the RunWorkerComple ted event?

Thanks for lookin in to this.

di***********@g mail.com schreef:
Hi,

the (.NET 2.0) backgroundworke r object has a DoWork method that
operates in a background thread. When the DoWork method is finished the
RunWorkerComple ted event is raised in the parent thread.

Now I have in the backgroundworke r's DoWork method a try catch finally
block, like this:

StreamReader sr = null;
try
{
sr = new StreamReader("b lahdieblah" +
e.Argument.ToSt ring());
if (!this.Cancella tionPending)
try
{
e.Result = sr.ReadToEnd();
}
catch (Exception exc)
{
throw exc;
}
else
{
e.Cancel = true;
e.Result = null;
}
}
catch (Exception exc)
{
throw exc;
}
finally
{
if (sr != null) sr.Close();
}
return;

The "blahdiebla h" part is to force an error. The 'e' is een event
argument that is passed to the DoWork method.
The error is catched, but code execution always breaks at the 'throw
exc' line.
I presumed that the error should have been propagated to the
RunWorkerComple tedEventArgs, in it's .Error property. However it never
gets there.

What did I miss? When and how will the .Error property be filled and
RunWorkerComple ted called, like the documentation states.
Sep 5 '06 #2

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

Similar topics

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.
8
4856
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
4
2351
by: Larry R | last post by:
I am trying to set a panel (that holds a progress image) to be visible when a long running process is happening. Sounds simple, right :) What happens is the panel never becomes visible. The load and prerender events occur, but it is never displayed. In the super simple example, I use the "hide panel", then click the "Do Process". The next thing I see is the "done". Why won't the panel display? Here is the code:
3
3200
by: John Wright | last post by:
I have a background worker thread that scans the network listing computers that are connected. I have a delegate that I call that invokes a procdure that loads a dataset with computer names and then attaches it to a dropdown. When I run it, my UI freezes until it completes. Any thoughts on how to do this without freezing the UI? John Code
5
11237
by: redear | last post by:
Is there a way to immediately terminate a BackgroundWorker thread? My problem is that the BackgroundWorker starts with a call to My.Computer.FileSystem.GetFiles that can run for a very long time if it is pointing to a directory tree with many files. If the user requests cancellation during this time, the main thread can call CancelAsync and can post a "Cancellation Pending" message to the user, but the BackgroundWorker cannot respond to...
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.
0
1901
by: PeterSchwennesen | last post by:
Problems starting a Timer Programmatically within a BackgroundWorker. I am trying to start a Timer inside a Backgroundworker. I want to start the BackGroundWorker and then have a timer tick a calculation reppeting inside the BackGroundWorker. The Code below seems to work ok for sending back the informaton "DoWork - Enter" and "DoWork - Exit, but the Timer "Timer_Ping" is newer ticked and the line "Time - Tick" is newer send back. Why is my...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
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...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.