Proper way to handle exceptions in worker threads in ThreadPool?

M

Mark Hoffman

All,

My application spawns a worker process by calling BeginInvoke with an
asynchronous callback method. This callback method is responsible for
calling EndInvoke. No problems there; pretty much straight out of MSDN.

My question is what happens when my worker thread raises an exception? I can
easily catch the exception, but I've noticed that unless my async callback
runs and EndInvoke gets called, then the worker thread never gets returned
to the threadpool. (Which makes perfectly good sense.)

So, how do I handle my exceptions in the worker thread so that EndInvoke is
called properly? Async callbacks get the IAsyncResult object passed to them
so it's easy to call EndInvoke, but I don't have that object inside my
worker thread, or at least I don't know how to get to it. So how can I call
EndInvoke when an exception is raised inside my worker thread?

Or am I just completely missing the boat here?
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

I wouldn't say that you are completely missing the boat. You indicate
correctly that the thread never gets returned to the threadpool if EndInvoke
is called. EndInvoke also serves to serve up the exception should one occur
on the thread.

Basically, you have to ensure that EndInvoke is always called. It's
going to happen somewhere, else, why call BeginInvoke and not care about the
result? It is just good practice to do so.

Hope this helps.
 
M

Mark Hoffman

Nicholas,

Thanks for the reply. I realized that EndInvoke must be called, but I was
thinking that my callback wasn't being called if an exception was raised in
the worker thread. If that was the case, I wondered how I would ever be able
to call EndInvoke since the callback was never called.

However...I incorrectly concluded that the async callback wasn't being
called if an exception was raised in the worker thread. I did some more
testing and I can see that the callback is in fact called, even if an
exception was raised in the worker thread.

Mark



Nicholas Paldino said:
Mark,

I wouldn't say that you are completely missing the boat. You indicate
correctly that the thread never gets returned to the threadpool if EndInvoke
is called. EndInvoke also serves to serve up the exception should one occur
on the thread.

Basically, you have to ensure that EndInvoke is always called. It's
going to happen somewhere, else, why call BeginInvoke and not care about the
result? It is just good practice to do so.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mark Hoffman said:
All,

My application spawns a worker process by calling BeginInvoke with an
asynchronous callback method. This callback method is responsible for
calling EndInvoke. No problems there; pretty much straight out of MSDN.

My question is what happens when my worker thread raises an exception? I can
easily catch the exception, but I've noticed that unless my async callback
runs and EndInvoke gets called, then the worker thread never gets returned
to the threadpool. (Which makes perfectly good sense.)

So, how do I handle my exceptions in the worker thread so that EndInvoke is
called properly? Async callbacks get the IAsyncResult object passed to them
so it's easy to call EndInvoke, but I don't have that object inside my
worker thread, or at least I don't know how to get to it. So how can I call
EndInvoke when an exception is raised inside my worker thread?

Or am I just completely missing the boat here?
 

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