By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

working example of WaitHandle.WaitAny() please

P: n/a
Can someone please post a working example of how to use the WaitAny()
method. Thanks.
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
pokémon,

Here is an example:

private mobjWaitHandle1 = new ManualResetEvent();
private mobjWaitHandle2 = new ManualResetEvent();

public void TestWaitAny()
{
// Start threads in the threadpool.
ThreadPool.QueueUserWorkItem(new WaitCallback(WaitCallbackHandler),
mobjWaitHandle1);
ThreadPool.QueueUserWorkItem(new WaitCallback(WaitCallbackHandler),
mobjWaitHandle2);

// Wait on the wait handles.
WaitHandle.WaitAny(new WaitHandle[]{mobjWaitHandle1, mobjWaitHandle2});
}

private void WaitCallbackHandler(object state)
{
// Create a randomizer and get a random number of seconds between 1 and
10 to wait.
Random pobjRandom = new Random();

// Wait on this thread for a number of seconds equal to the number
passed in through the state.
Thread.Sleep(pobjRandom.Next(1, 10) * 1000);

// Signal the wait handle passed in.
((ManualResetEvent) state).Set();

// Get out.
return;
}

Basically, two threads are started and they both wait for a random
number of seconds between 1 and 10. Once they are done, they signal the
event that is passed in. The main thread calling the threading code waits
until one of the threads signals through the event.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"pokémon" <po**@mon.com> wrote in message
news:P6******************@nwrdny02.gnilink.net...
Can someone please post a working example of how to use the WaitAny()
method. Thanks.

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.