Ken Kolda
8/23/2004 2:58:00 PM
Don't use WaitForSingleObject(). Instead, just wrap your function in a
delegate and use the BeginInvoke() method of the delegate. This will cause
the function to be executed on a separate thread (actually, one of the
thread pool threads). WaitForSingleObject() is really appropriate when you
have an async task, but what you're trying to implement are tasks that
execute synchronously but concurrently (on multiple threads).
Ken
"source" <sourcecode116@hotmail.com> wrote in message
news:Otk%23emLiEHA.2916@TK2MSFTNGP12.phx.gbl...
> How will I be able to use a delegate with WaitForSingleObject
> as it only takes WaitOrTimerCallback
> and moreover WaitForSingleObject returns only RegisteredWaitHandle
> As I am new to threading, your input will be helpful.
>
>
> "Ken Kolda" <ken.kolda@elliemae-nospamplease.com> wrote in message
> news:OqSleUghEHA.3428@TK2MSFTNGP11.phx.gbl...
> > Instead of using the ThreadPool directly (i.e. calling
> > RegisterWaitForSingleObject), use a delegate instead so you can get back
a
> > WaitHandle for each call to Function C. Then use WaitHandle.WaitAll() to
> > wait until all of the Function C calls have finished, e.g.
> >
> > Function B
> > {
> >
> > for (int i = 0; i < 3; i++)
> > {
> > IAsyncResult res = new MyFunctionCDelegate(Function
> > C).BeginInvoke(...);
> > handles[i] = res.AsyncWaitHandle;
> > }
> >
> > WaitHandle.WaitAll(handles);
> > }
> >
> > (Sorry for the shift to C# -- my VB.NET is pretty weak). Hope that
helps.
> >
> > Ken
> >
> >
> > "Abhi" <sourcecode116@hotmail.com> wrote in message
> > news:uK$3RfZhEHA.3944@tk2msftngp13.phx.gbl...
> > > I have a console app. which has a timer which is set to fire every 30
> > > seconds.
> > >
> > > The function which the timer calls then calls another function using
> > > ThreadPool
> > >
> > > Function A
> > > {
> > > Timer Declared with 30 seconds interval
> > > Timer.Change(Infinite,Infinite) //Timer stopped for infite time
> > > Timer calls Function B()
> > > Timer.Change(1,30000) //Timer again set to fire every 30
seconds
> > > }
> > >
> > > Function B
> > > {
> > > For a=0 to 3
> > > {
> > > ThreadPool.RegisterWaitForSingleObject(Function C)
> > > }
> > > }
> > >
> > > Function C
> > > {
> > > Takes 40-50 seconds to process
> > > }
> > >
> > > Now my problem is I dont want the Timer (declared in Function A) to
fire
> > > unless and until Function C gets completed.
> > > What can i do in Function B to stop the control going back to Function
> A,
> > or
> > > is there someway I can find out if all the threads executing are
> finished
> > > processing.
> > > So in my case 3 threads are on loose as I am in a for loop, I dont
want
> to
> > > go back to function A unless and until I am done processing with those
3
> > > threads.
> > >
> > >
> >
> >
>
>