views:

131

answers:

4

Final question for today :) thanks for your input on the previous ones. BTW: Already searched the forum on this and nothing quite answers this one.

We use some 3rd party libraries which pop work onto the Threadpool and we don't want to shutdown while there are outstanding activities.

When shutting down an application, the application will shutdown even if there is work outstanding in the System.Threading.ThreadPool because these threads are back ground threads.

Other than doing some form of reference counting (and enforcing every item Queued into the pool to use this mechanism, which we don't have control over) is there some way to stop the application shutting down while there is outstanding work to be done.

I've looked at using the GetAvailableThreads() vs GetMaxThreads(), but these can be the same because we may have caught the Threadpool as a thread was freed up but not allocated a queued workitem.

All help appreciated?

Kind Regards Noel

+1  A: 

Unless you can get some kind of callback into the 3rd-party code (maybe a completion event), you are going to struggle to know when they have stopped using the ThreadPool. Sorry. Such code should typically be on a non-background thread. I realise you don't have control over this - I'm just not sure that there is an easy way out of the hole, either.

Marc Gravell
Thanks Marc. Though it might be an awkward one.
Bigtoe
A: 

I think Marc Gravell is right. Though if you could get access to the threads being used in the thread pool somehow then you could change the Thread.IsBackground property to false to achieve what you want. But I am not sure how you would obtain a reference to a managed thread within the thread pool.

SDX2000
+1  A: 

Since I'm real programmer (that's C, boys ;-) I've not used ThreadPool.

However, a suggestion comes to mind - have a semaphore increment on each task you issue into threadpool, and that that semaphore be released on completion.

When your app comes to exit, wait on the semaphore.

Any use?

Blank Xavier
Thanks, but it's a 3rd party library and don't have access to their code to modify things for reference counting etc.
Bigtoe
Presumably there's a notification of some sort when a given task completes? you don't need to mod their code - it's your semaphore, you increment on the way in, then you release when a completion occurs. Wait on the semaphore when your process wants to finish.
Blank Xavier
A: 

Hi Folks, Thanks for your replies. I've found a work-around outside of the thread pool using AOP and Castle Windsors dynamic proxy to inject referencing counting around the class. Luckily for me the class exposed a clean interface that I could wrap and it works a treat.

Bigtoe