I'm working with the following components:
I'm invoking the library method by using a backgroundworker. The library throws the exception but the RunWorkerCompleted handler is never called.
The only way to catch the exception is to surround my DoWork handler code with a try/catch block.
Did is misunderstand the RunWorkerCompletedEventArgs.Error Property? Isn't it for getting exceptions which got caught by the BackgroundWorker?
Codesample:
static BackgroundWorker w = new BackgroundWorker();
w.DoWork += new DoWorkEventHandler(w_DoWork);
w.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(w_RunWorkerCompleted);
w.RunWorkerAsync();
static void w_DoWork(object sender, DoWorkEventArgs e)
{
MyClass m = new MyClass();
w.result = m.Compute();
}
static void w_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
HandleException(e.Error);
}
/* result related code */
}
static void HandleException(Exception e)
{
ExceptionPolicy.HandleException(e, "MyPolicy");
}
The above sample leads to a termination of my console application. The vs2010 output writes absolutely nothing (only default output).
So where's the problem?
//Edit: this snippet works for catching the library's exception.
static void w_DoWork(object sender, DoWorkEventArgs e)
{
try
{
MyClass m = new MyClass();
w.result = m.Compute();
}catch(Exception e){ }
}
The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running.
BackgroundWorker makes the implementation of threads in Windows Forms. Intensive tasks need to be done on another thread so the UI does not freeze. It is necessary to post messages and update the user interface when the task is done.
A BackgroundWorker component executes code in a separate dedicated secondary thread. In this article, I will demonstrate how to use the BackgroundWorker component to execute a time-consuming process while the main thread is still available to the user interface.
BackgroundWorker, is a component in . NET Framework, that allows executing code as a separate thread and then report progress and completion back to the UI.
That is the correct pattern for BackgroundWorker.
I suspect the problem is that your Main
method is exiting before the BW has completed.
RunWorkerAsync
will return immediately and if you are not waiting in Main
, then your process will end, perhaps even before the BW has started, never mind completed.
Try adding a Console.ReadLine
at the end of your Main
method.
Out of interest:
BW behaves differently in a Console app and a Windows app. If you use a WinForms or WPF app, there will be a derived SynchronizationContext on your UI thread and BW will marshal RunWorkerCompleted
back to the UI thread and run it there. That's one of the main benefits of BW.
In a Console App, the default SynchronizationContext is used and this marshals RunWorkerCompleted
onto a thread pool thread. This means you can block the Main
thread and the completed handler will still run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With