Home All Groups Group Topic Archive Search About

Wait for all ThreadPool workers to complete

Author
16 Aug 2006 9:36 PM
Michael D. Ober
I have an application that needs to limit the number of simultaneous threads
that are executing at any given time.  The threadpool appears to be ideal
for this.  However, I need to have my Sub Main wait until all queued workers
are complete.  Is there anyway to do this short of polling a counter?

Thanks,
Mike Ober.

Author
17 Aug 2006 7:11 AM
Vadym Stetsyak
Hello, Michael!

MDO> I have an application that needs to limit the number of simultaneous
MDO> threads that are executing at any given time.  The threadpool appears
MDO> to be ideal for this.  However, I need to have my Sub Main wait until
MDO> all queued workers are complete.  Is there anyway to do this short of
MDO> polling a counter?

AFAIK there is no built-in mechanism in ThreadPool class.
However, you can introduce an array of events that will be set in the
worker threads, while main thread will wait for them.

pseudo code can look like this

//global events list
List<AutoResetEvent> events = new List<AutoResetEvent>();

void ThreadWorker()
{
    AutoResetEvent finishEvent = new AutoResetEvent(false);

    //do some job here

    //notify waiting thread
    finishEvent.Set();
}

void ThreadThatIsWaitingForWorkers()
{

     AutoResetEvent[] waitEvents =  events.ToArray();

    //wait for all workers to finish
    WaitHandle.WaitAll(waitEvents);

}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
17 Aug 2006 12:46 PM
Michael D. Ober
Sounds like time to write my own thread/connection pool for the server.  The
problem is that too many processor intensive threads on the server and it
bogs down.

Thanks,
Mike.

Show quote
"Vadym Stetsyak" <vady***@ukr.net> wrote in message
news:%23qHDmvcwGHA.5044@TK2MSFTNGP05.phx.gbl...
> Hello, Michael!
>
> MDO> I have an application that needs to limit the number of simultaneous
> MDO> threads that are executing at any given time.  The threadpool appears
> MDO> to be ideal for this.  However, I need to have my Sub Main wait until
> MDO> all queued workers are complete.  Is there anyway to do this short of
> MDO> polling a counter?
>
> AFAIK there is no built-in mechanism in ThreadPool class.
> However, you can introduce an array of events that will be set in the
> worker threads, while main thread will wait for them.
>
> pseudo code can look like this
>
> //global events list
> List<AutoResetEvent> events = new List<AutoResetEvent>();
>
> void ThreadWorker()
> {
>    AutoResetEvent finishEvent = new AutoResetEvent(false);
>
>    //do some job here
>
>    //notify waiting thread
>    finishEvent.Set();
> }
>
> void ThreadThatIsWaitingForWorkers()
> {
>
>     AutoResetEvent[] waitEvents =  events.ToArray();
>
>    //wait for all workers to finish
>    WaitHandle.WaitAll(waitEvents);
>
> }
>
> --
> Regards, Vadym Stetsyak
> www: http://vadmyst.blogspot.com
Author
17 Aug 2006 12:56 PM
Vadym Stetsyak
Hello, Michael!

You can limit ThreadPool worker thread count.
Or if your server uses sockets you can use async io, like
BeginXXX/EndXXX

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
17 Aug 2006 4:27 PM
sloan
If youre using 2.0 you can limit the total used threads on the ThreadPool.

1.1... there isn't something built in.

I've scoured the internet looking for something like that.

Maybe look at this:
http://dotnetjunkies.com/Tutorial/D7E688B8-0BDD-4D44-9A0F-4CD26FB35F51.dcik


Show quote
"Vadym Stetsyak" <vady***@ukr.net> wrote in message
news:OumxwwfwGHA.1624@TK2MSFTNGP02.phx.gbl...
> Hello, Michael!
>
> You can limit ThreadPool worker thread count.
> Or if your server uses sockets you can use async io, like
> BeginXXX/EndXXX
>
> --
> Regards, Vadym Stetsyak
> www: http://vadmyst.blogspot.com
Author
17 Aug 2006 4:38 PM
Michael D. Ober
My server is written in VMS Basic, which requires a separate process for
each connection.  It's those processes I have to manage from the client
side.

Mike.

Show quote
"sloan" <sl***@ipass.net> wrote in message
news:%23NiGhohwGHA.5064@TK2MSFTNGP06.phx.gbl...
>
> If youre using 2.0 you can limit the total used threads on the ThreadPool.
>
> 1.1... there isn't something built in.
>
> I've scoured the internet looking for something like that.
>
> Maybe look at this:
> http://dotnetjunkies.com/Tutorial/D7E688B8-0BDD-4D44-9A0F-4CD26FB35F51.dcik
>
>
> "Vadym Stetsyak" <vady***@ukr.net> wrote in message
> news:OumxwwfwGHA.1624@TK2MSFTNGP02.phx.gbl...
>> Hello, Michael!
>>
>> You can limit ThreadPool worker thread count.
>> Or if your server uses sockets you can use async io, like
>> BeginXXX/EndXXX
>>
>> --
>> Regards, Vadym Stetsyak
>> www: http://vadmyst.blogspot.com
>
>
Author
17 Aug 2006 11:58 PM
Greg Young
and I thought thread per process was unscalable :D


Show quote
"Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message
news:uhemfuhwGHA.2400@TK2MSFTNGP06.phx.gbl...
> My server is written in VMS Basic, which requires a separate process for
> each connection.  It's those processes I have to manage from the client
> side.
>
> Mike.
>
> "sloan" <sl***@ipass.net> wrote in message
> news:%23NiGhohwGHA.5064@TK2MSFTNGP06.phx.gbl...
>>
>> If youre using 2.0 you can limit the total used threads on the
>> ThreadPool.
>>
>> 1.1... there isn't something built in.
>>
>> I've scoured the internet looking for something like that.
>>
>> Maybe look at this:
>> http://dotnetjunkies.com/Tutorial/D7E688B8-0BDD-4D44-9A0F-4CD26FB35F51.dcik
>>
>>
>> "Vadym Stetsyak" <vady***@ukr.net> wrote in message
>> news:OumxwwfwGHA.1624@TK2MSFTNGP02.phx.gbl...
>>> Hello, Michael!
>>>
>>> You can limit ThreadPool worker thread count.
>>> Or if your server uses sockets you can use async io, like
>>> BeginXXX/EndXXX
>>>
>>> --
>>> Regards, Vadym Stetsyak
>>> www: http://vadmyst.blogspot.com
>>
>>
>
>
Author
18 Aug 2006 3:36 PM
Michael D. Ober
It appears to scale just fine on VMS.

Mike Ober.

Show quote
"Greg Young" <druckdruckREMOVEgo***@hotmail.com> wrote in message
news:u9nZmklwGHA.3264@TK2MSFTNGP03.phx.gbl...
> and I thought thread per process was unscalable :D
>
>
> "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message
> news:uhemfuhwGHA.2400@TK2MSFTNGP06.phx.gbl...
>> My server is written in VMS Basic, which requires a separate process for
>> each connection.  It's those processes I have to manage from the client
>> side.
>>
>> Mike.
>>
>> "sloan" <sl***@ipass.net> wrote in message
>> news:%23NiGhohwGHA.5064@TK2MSFTNGP06.phx.gbl...
>>>
>>> If youre using 2.0 you can limit the total used threads on the
>>> ThreadPool.
>>>
>>> 1.1... there isn't something built in.
>>>
>>> I've scoured the internet looking for something like that.
>>>
>>> Maybe look at this:
>>> http://dotnetjunkies.com/Tutorial/D7E688B8-0BDD-4D44-9A0F-4CD26FB35F51.dcik
>>>
>>>
>>> "Vadym Stetsyak" <vady***@ukr.net> wrote in message
>>> news:OumxwwfwGHA.1624@TK2MSFTNGP02.phx.gbl...
>>>> Hello, Michael!
>>>>
>>>> You can limit ThreadPool worker thread count.
>>>> Or if your server uses sockets you can use async io, like
>>>> BeginXXX/EndXXX
>>>>
>>>> --
>>>> Regards, Vadym Stetsyak
>>>> www: http://vadmyst.blogspot.com
>>>
>>>
>>
>>
>
>

AddThis Social Bookmark Button