473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

working example of WaitHandle.WaitAny() please

Can someone please post a working example of how to use the WaitAny()
method. Thanks.
Nov 15 '05 #1
1 19097
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
Is there any way to find out: 1. which thread is holding a monitor object? 2. how many Monitor.Enter() has been called on an object? 3. which WaitHandle is signaled after WaitHandle.WaitAny()? ...
1
by: Bob Rock | last post by:
Hello, did anyone notice that the WaitHandle class is not abstract as the ..NET SDK documentation says? Bob Rock
1
by: Larry Lard | last post by:
Just now I translated Jon Skeet's WaitHandle.WaitAny / .WaitAll (<http://yoda.arachsys.com/csharp/threads/waithandles.shtml>) from C# into VB.NET, and after the first go I ran into trouble: ...
0
by: Amir Shitrit | last post by:
Hi to all. What is difference between the methods found in the Monitor, Interlocked and WaitHandle classes in terms of performance? Is it true that Monitor.Wait uses the Interlocked class behind...
1
by: Sean | last post by:
I am looking at using delegates to perform an asynchronous call. There will be a callback invoked at the end of the call. The client thread will wait on the WaitHandle from IAsyncResult (which is...
0
by: William Stacey [C# MVP] | last post by:
I am looking to do my own WaitAny. Have N producer objects that have internal consumer Q. Can do blocking read or async read (using own asyncresult object) of any 1 producer - that works fine. ...
14
by: Dave Booker | last post by:
I'm doing some analysis that is readily broken up into many independent pieces, on a multicore machine. I thought it would be best to just queue like 1000 of these pieces in the ThreadPool, and...
1
by: archana | last post by:
Hi all, can anyone tell me exact meaning of exitcontext of waithandle.waitany. What exactly synchronization domain means? i am having one windows application in which i creating some thread...
1
by: archana | last post by:
Hi all, can anyone tell me exact meaning of exitcontext of waithandle.waitany. What exactly synchronization domain means? i am having one windows application in which i creating some thread...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.