Execption handling for Thread

G

Guest

Hi:

I will appreciate your comment on the followings:

From the main program I start threads. After starting the thread the main
program do some other processing. If any of the threads throws any error
during their processing, I want to capture that exception from host and log
it.

Is it possible to capture the exception that a thread throws from the main
program?

Thanks
 
J

Jon Skeet [C# MVP]

pothik05 said:
I will appreciate your comment on the followings:

From the main program I start threads. After starting the thread the main
program do some other processing. If any of the threads throws any error
during their processing, I want to capture that exception from host and log
it.

Is it possible to capture the exception that a thread throws from the main
program?

Have a look at AppDomain.UnhandledException. Of course, if you control
the other threads yourself, you could make the top-level method that
the thread starts with have a big try/catch which can feed the
exception back to the main thread (or log it, or whatever).
 
G

Guest

Hi Jon

Thanks for your reply. I can have a big try/catch in the method that starts
the thread and log the exception. But how will I "feed the exception back to
the main thread"? I can raise an event to notify the main program, but is
there any other way to catch any unhandled exception of the thread from the
main program.
 
J

Jon Skeet [C# MVP]

pothik05 said:
Thanks for your reply. I can have a big try/catch in the method that starts
the thread and log the exception. But how will I "feed the exception back to
the main thread"? I can raise an event to notify the main program, but is
there any other way to catch any unhandled exception of the thread from the
main program.

No, you can't *catch* the exception in a different thread, but I'm not
sure why you'd want to, if all you need to do is log it.

To tell the main thread about the exception, just do the same as you
would for anything else which needs to get back to the UI thread - use
Control.Invoke or Control.BeginInvoke.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top