MrZap

Hello,
I don't know how can i handle if any error occured in my open thread. And i also used delegate method in my thread. I already tried Application.ThreadException And AppDomain.CurrentDomain.UnhandledException but there is no luck.



Re: Visual C# General How can i handle Exception If error occured in Thread?

Matthew Watson

I can think of a couple of ways:

(1) Expose an event from your user interface class that your thread code can access. Then wrap your entire thread code in a try/catch and if you catch an exception, fire off the event.

(2) Something like this:


private void button1_Click( object sender, System.EventArgs e )
{
ThreadDelegate td = new ThreadDelegate( thread );

// Passing ¡®td¡¯ to BeginInvoke() will cause it to be passed to the callback
// function via the IAsyncResult.AsyncState field.

td.BeginInvoke( new AsyncCallback( thread_callback ), td );
}

private void thread_callback( IAsyncResult result )
{
MessageBox.Show( "Thread terminated" );

// result.AsyncState is the object parameter that was passed to BeginInvoke().
// The thread delegate itself being invoked was passed, so we can just cast
// it here¡­

ThreadDelegate td = (ThreadDelegate) result.AsyncState ;

try
{
td.EndInvoke( result ); // EndInvoke ¡°remembers¡± any thread exception.
}

catch ( Exception ex )
{
MessageBox.Show( "Thread threw an exception: " + ex.Message );
}
}

private void thread()
{
System.Threading.Thread.Sleep( 1000 );
throw new Exception( "BANG!" );
}

private delegate void ThreadDelegate();







Re: Visual C# General How can i handle Exception If error occured in Thread?

Matthew Watson

Actually, ignore that #2 - it won't really help you.






Re: Visual C# General How can i handle Exception If error occured in Thread?

Peter Ritchie

It depends on the type of thread. Unhandled exceptions on some threads are simply swallowed and you'll never be informed. Asynchronous delegates are a good example. Methods using the .NET 1.x asynchronous pattern (BeginXXX/EndXXX) are another; any unhandled exceptions will be swallowed until the call to EndInvoke (if you don't have an EndInvoke the exception never propagates to ThreadException or UnhandledException).

I would question why you want to handle thread exceptions in this way. If it's to inform the user somehow; you really have no way of knowing if the UI is capable of providing information to the user (ThreadException and UnhandledException handlers are called on the thread that caused the exception).






Re: Visual C# General How can i handle Exception If error occured in Thread?

MrZap

Actually there is one delegate method i am calling in my thread. now some how error occured in delegate and that will return in thread but now i don't know that how can i pass that error to my main application




Re: Visual C# General How can i handle Exception If error occured in Thread?

Peter Ritchie

MrZap wrote:
Actually there is one delegate method i am calling in my thread. now some how error occured in delegate and that will return in thread but now i don't know that how can i pass that error to my main application
In what way do you expect to be able to arbitrarily interrupt your main/GUI thread with information that an exception has occurred on another thread




Re: Visual C# General How can i handle Exception If error occured in Thread?

Matthew Watson

The nearest to interrupting it is mainform.Invoke(), I guess.




Re: Visual C# General How can i handle Exception If error occured in Thread?

Peter Ritchie

Matthew Watson wrote:
The nearest to interrupting it is mainform.Invoke(), I guess.
The issue isn't what method to call in order to inject code into the GUI thread; the issue is there is no way of telling if the GUI thread is in a state where it can handle informing the user of something.

Let's say the use clicked some object on the screen and has dragged it to begin a drag-drop operation. Control.Invoke is more than happy to interrupt the GUI thread and stick some message box up there that the user will have to release the mouse button--aborting the drag operation--in order to click some OK button. Who knows what state that leaves the application Another way of classically getting an application in a weird state is to corresponding mouse down and mouse up messages occur on different windows. If the Control.Invoke occured after a mouse down but before the up the application likely doesn't take the possibility of two mouse down events occurring without having a mouse up in between.






Re: Visual C# General How can i handle Exception If error occured in Thread?

vtortola

Try:

Code Snippet
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Regards.
 





Re: Visual C# General How can i handle Exception If error occured in Thread?

Matthew Watson

But is handling something like that any different to handling any other kind of event in the main UI thread For example, isn't handling a BackgroundWorker.RunWorkerCompleted event just the same




Re: Visual C# General How can i handle Exception If error occured in Thread?

Peter Ritchie

Matthew Watson wrote:
But is handling something like that any different to handling any other kind of event in the main UI thread For example, isn't handling a BackgroundWorker.RunWorkerCompleted event just the same
BackgroundWorker uses Control.BeginInvoke not Control.Invoke when dealing with forms/controls. Using BeginInvoke would mitigate issues of interrupting a series of messages that don't really support interrupting. A RunWorkerCompleted event handler would suffer the same problem with things like Drag-Drop, should the application not be in a state disabling certain functionality if the RunWorkerCompleted event handler brought up a message box.

The difference between a BackgroundWorker and handling a RunWorkerCompleted event is that RunWorkerCompleted reasonably deterministic; you know when the background worker is "running" and a message box could/will be displayed. Arbitrarily showing a message box on thread exceptions would include all thread pool threads--which are used by the framework--for which you can't determine are "running" and could throw an exception that could bring up a message box.