WaitAll with threadpool

  • Thread starter Thread starter Shad
  • Start date Start date
S

Shad

Hello,
here's my problem (I've been searching for a fews days already and couldn't
find a solution)

I've got a bunch of mails to create, so I'd like to queue jobs in a
threadpool (which I can do already), but once all jobs are queued, I'd like
the main thread to wait until all mails are generated.
So far I haven't found how to do that, since the threadpool doesn't provide
an adapted WaitAll method.
Could anyone help please ?
Thanks
 
Hi Shad
\\\\
Threading.Thread.Sleep(10000) ' 10 seconds
/////
I hope this helps a little bit?
Cor
 
Hi Shad
\\\\
Threading.Thread.Sleep(10000) ' 10 seconds
/////
I hope this helps a little bit?
Cor

Hmm nope,
each mail might take up to 10 seconds to generate and i can have up to
1500 of them.
So what I'd really like is a way to know when they're all done
(something else than just counting how many threads I create and then
decrementing each time working thread finishes (I can detect that with
an event)).
I have found on the web a component that implements WaitAll for a
threadpool, but it's in C# and I'd really like toi stick to VB and
understand what I'm doing.

Or is it possible to queue my main thread at the end of the threadpool,
so it takes back control when all others threads are done ?

Thanks for the tip anyway
 
Hi Shad,
I did not want add it this time, mostly I set this in a loop as answer
\\\\
do while eventnotbeen
application.doevents
threading.thread.sleep(100)
loop
/////

Cor
 
Hi Shad,
I did not want add it this time, mostly I set this in a loop as answer
\\\\
do while eventnotbeen
application.doevents
threading.thread.sleep(100)
loop
/////

Cor

But it's still the same problem, the point is that I don't have only one
working thread, so this cannot be done, unless there is an event raised
when the threadpool completes its last job, is that what you're saying ?
 
Shad,
I do it in that way yes, keep track wich threads are active, therefore I
just have a counter
when the counter is zero all threads are done.
Cor
 
Shad,
I do it in that way yes, keep track wich threads are active, therefore I
just have a counter
when the counter is zero all threads are done.
Cor

I see.
I was just hoping there'd be a better solution, but I guess that'll have to
do.
Thanks a lot for your help
 
Shad,
Create a class that represents each request going into the ThreadPool (if
you have not already done so).

One member of this class is a System.Threading.ManualResetEvent object.

Your main thread needs to have an array of the ManualResetEvent objects in
each request. The main thread does a WaitHandle.WaitAll on this array of
ManualResetEvent objects. Remember that ManualResetEvent inherits from
WaitHandle so it has the WaitAll method also.

Each queue job sets its ManualResetEvent when it is finished.

Of course the above means that the main thread will be blocking until all
the queue jobs are done. If the main thread is a UI thread this is NOT
desirable!

Hope this helps
Jay
 
Herfried K. Wagner said:
The number of "\" and "/" should be matching.

SCNR

Aaaaaaahhhhh... ;) I read the statement 10 times and didn't find a division
operator...
 
Shad,
Create a class that represents each request going into the ThreadPool
(if you have not already done so).

One member of this class is a System.Threading.ManualResetEvent
object.

Your main thread needs to have an array of the ManualResetEvent
objects in each request. The main thread does a WaitHandle.WaitAll on
this array of ManualResetEvent objects. Remember that ManualResetEvent
inherits from WaitHandle so it has the WaitAll method also.

Each queue job sets its ManualResetEvent when it is finished.

Of course the above means that the main thread will be blocking until
all the queue jobs are done. If the main thread is a UI thread this is
NOT desirable!

Hope this helps
Jay

This is more like what i was looking for.
Thanks Jay, I'll try it
 
Back
Top