Wednesday, August 01, 2007

Exceptions from Threads in Threadpool

I've had a question in a comment of EventType clr20r3 From Windows Service from quantboy:

if a thread from the threadpool throws, can you prevent that exception from taking down the entire application by wrapping the code that thread runs within a try-catch block?

Simple answer from my experience is yes. The only time we've seen a thread take down an application is when it throws something out of the method that the thread is running. Simply wrapping that code in a try...catch allows the thread to stop gracefully, and if you place some logging in the catch block you'll get much more meaningful errors.

Then you should be able to work towards fixing why the method is throwing in the first place :).

No comments: