473,805 Members | 2,055 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 2909
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
12114
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
4857
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
3201
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
11238
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
2483
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
1902
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
9718
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
9596
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
10614
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...
0
10363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10109
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...
0
9186
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...
0
6876
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.