O
orekin
Hi There
I have been programming C# for a couple of months and am trying to
master Threading.
I understand that ThreadPool uses background threads (see code example
in MSDN page titled 'ThreadPool Class [C#]') .... however I would like
to ensure that all my ThreadPool threads have completed before I exit
my Main function.
At the moment I am using this code in my main function:
ThreadPool.GetMaxThreads(out maxThreads,out
maxPortCompletionThreads);
do
{
Thread.Sleep(100);
ThreadPool.GetAvailableThreads(out workerThreads,out
portCompletionThreads);
}while(maxThreads!=workerThreads||maxPortCompletionThreads!=portCompletionThreads);
I use the above code right at the end of my main function, and it
seems to do the job of ensuring that all ThreadPool threads finish
before the main function completes (and consequently the application
finishes).
I am aware that I do not 'have to' use the ThreadPool - I could start
my own threads up then join the main thread to the own threads.
However I would prefer to stick to the ThreadPool if I can.
My question is simply 'is there a better way to ensure that all
ThreadPool threads complete before exiting the main function' ? The
code I am writing at the moment is part of a large project and I want
to make my contribution fairly standard (rather than have other
programmers look at my work and say 'what the heck is he doing here?!?
I have not seen this before!) ...
Thanks In Advance
Bill
I have been programming C# for a couple of months and am trying to
master Threading.
I understand that ThreadPool uses background threads (see code example
in MSDN page titled 'ThreadPool Class [C#]') .... however I would like
to ensure that all my ThreadPool threads have completed before I exit
my Main function.
At the moment I am using this code in my main function:
ThreadPool.GetMaxThreads(out maxThreads,out
maxPortCompletionThreads);
do
{
Thread.Sleep(100);
ThreadPool.GetAvailableThreads(out workerThreads,out
portCompletionThreads);
}while(maxThreads!=workerThreads||maxPortCompletionThreads!=portCompletionThreads);
I use the above code right at the end of my main function, and it
seems to do the job of ensuring that all ThreadPool threads finish
before the main function completes (and consequently the application
finishes).
I am aware that I do not 'have to' use the ThreadPool - I could start
my own threads up then join the main thread to the own threads.
However I would prefer to stick to the ThreadPool if I can.
My question is simply 'is there a better way to ensure that all
ThreadPool threads complete before exiting the main function' ? The
code I am writing at the moment is part of a large project and I want
to make my contribution fairly standard (rather than have other
programmers look at my work and say 'what the heck is he doing here?!?
I have not seen this before!) ...
Thanks In Advance
Bill