ashk1860

hi

I have a windows application which have much intraction with the webservices. so I use backgroundworkers to make connection with webservices. I mostly handle DoWork and RunWorkerCompleted events to get the data in one and set in the other. and application is in debuging step now.

Just assume that the user loose the connection to internet when a backgroundworker is trying to connect to the server. I can catch the error but It's not enough. I also want to prevent RunWorderCompleted event if the worker could not get the right data from the internet. but I don't know how! throwing exceptions from worker's thread to the current thread seems imposible (tried it unsuccessfully) canceling the worker will not prevent RunworkerCompleted event to run. I am sure that every one who have experience to make such a program would use multi threads but I dont know how to handle the errors like this...

thank you in advance

regards



Re: Visual C# General debuging multi thread application problem

Peter Ritchie

When you throw (or don't catch) and exception in the DoWork event it gets saved and will rethrow in the RunWorkerCompleted event, as a System.Reflection.TargetInvocationException when you try to get the result. The original exception is stored in the InnerException property. This is the ideal way of doing what you want because it gets the exception back to the GUI thread and you can then reliably inform the user of the problem via the GUI.

For example:

Code Snippet

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

throw new System.Net.WebException();

}

Which can be caught in the completed event handler:

Code Snippet

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

try

{

String text = e.Result as String;

}

catch (System.Reflection.TargetInvocationException targetInvocationException)

{

MessageBox.Show(targetInvocationException.InnerException.GetType().FullName + " thrown while communicating with Web Service", "Error");

}

}

...note the use of the InnerException.






Re: Visual C# General debuging multi thread application problem

ashk1860

thank you for your helpful reply. so I should ask one other question(don't mark as answer yet to don't loose attention). I am trying to throw all the exeptions to the main method and handle them there. but when the error occur the program will stop in thow line instead of going to the main. note that I manage to have try and catch in all of my blocks and throw all of them except main method but it don't go there. this was the reason that I thought it might be the threading problem





Re: Visual C# General debuging multi thread application problem

ashk1860

oops! I have tried your code but the program stops in throwing line without catching in the RunWorkerCompleted event

throw new System.Net.WebException();





Re: Visual C# General debuging multi thread application problem

Peter Ritchie

ashk1860 wrote:

oops! I have tried your code but the program stops in throwing line without catching in the RunWorkerCompleted event

throw new System.Net.WebException();

Do you mean the program breaks on the line with the exception in the DoWork handler or the application exits because of the exception in the DoWork handler (i.e. the framework exception dialog is displayed and details the WebException (not the TargetInvocationException)




Re: Visual C# General debuging multi thread application problem

Peter Ritchie

ashk1860 wrote:
thank you for your helpful reply. so I should ask one other question(don't mark as answer yet to don't loose attention). I am trying to throw all the exeptions to the main method and handle them there. but when the error occur the program will stop in thow line instead of going to the main. note that I manage to have try and catch in all of my blocks and throw all of them except main method but it don't go there. this was the reason that I thought it might be the threading problem

Do you have the following in your app.config

Code Snippet

<legacyUnhandledExceptionPolicy enabled="1"/>






Re: Visual C# General debuging multi thread application problem

ashk1860

yes, the program breaks on the line with the exception in the DoWork handler





Re: Visual C# General debuging multi thread application problem

Peter Ritchie

ashk1860 wrote:
yes, the program breaks on the line with the exception in the DoWork handler

That's normal if you have the debugger set to break on exceptions when they are thrown.




Re: Visual C# General debuging multi thread application problem

ashk1860

I check the app.config I didn't have that line. then I checked with and without it. nothing changed. how can I set the debugger to don't breake on exceptions when they are thrown it's strang because when I check throwing in my application, they just don't thrown when they are in DoWorker event.





Re: Visual C# General debuging multi thread application problem

Peter Ritchie

In the main menu, select Debug/Exceptions (Ctrl+D,E if you have the C# keyboard layout). The checkbox beside Common Language Runtime Exceptions will be checked to denote the debugger will break on any thrown exception. If only some exceptions are checked this checkbox will be unchecked. You can expand Common Language Runtime Exceptions, then expand System.Net to see if System.Net.WebException is checked. If it is checked, uncheck it.

The legacyUnhandledExceptionPolicy config setting will cause your application to terminate when an exception is thrown from the DoWork handler (which is the way .NET 1.1 worked).






Re: Visual C# General debuging multi thread application problem

ashk1860

it is just like what it should be! it don't work when it is in the other thead not with special exception. I mean I check it with trying to divide by zero in doWorder and in the main thread. it will thow in current thread and breaks in DoWorker event! totally different behavior with the same exception





Re: Visual C# General debuging multi thread application problem

Peter Ritchie

With no Common Language Runtime Exceptions checked in the Exceptions dialog and no legacyUnhandledExceptionPolicy element in app.config, I cannot reproduce what you describe. Catching TargetInvocationException in the Completed event handler always catches any exception thrown in the DoWork handler, be it a divide by zero or a WebException. You have something like this for your Completed event handler :

Code Snippet

private void backgroundWorker1_RunWorkerCompleted ( object sender, RunWorkerCompletedEventArgs e )

{

try

{

Object result = e.Result;

}

catch (System.Reflection.TargetInvocationException)

{

System.Diagnostics.Debug.WriteLine("ding");

}

}






Re: Visual C# General debuging multi thread application problem

ashk1860

here is one of my testing couple of events:

Code Snippet

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

try

{

int i=0;

while (i < 10)

{

int t = 1 / i - 3;

++i;

}

}

catch

{

throw new System.Net.WebException();

}

}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

try

{

String text = e.Result as String;

}

catch (Exception ex)

{

MessageBox.Show(ex.InnerException.GetType().FullName + " thrown while communicating with Web Service", "Error");

}

}

and I expected to get the messagebox instead of breaking the application!

I just copy and paste the codes in DoWorder to the test buttonClick event I have been thrown to main cleanly!!!





Re: Visual C# General debuging multi thread application problem

Peter Ritchie

I can't reproduce what you've detailed with the code you've provided. When I run your code it always only breaks in the Completed event handler at the catch.

Also, the only exception throw when you access the RunWorkerCompletedEventArgs.Result is TargetInvocationException. I recommend not catching "Exception" and only catching "TargetInvocationException"; but that has no bearing on what you're seeing.






Re: Visual C# General debuging multi thread application problem

ashk1860

Weird! I use VS 2005