views:

1000

answers:

2

1)The call AsyncWaitHandle.WaitOne may block client or will definitely block the client?.

2)What is the difference between WaitAll,WaitOne,WaitAny?

+2  A: 

The methods allow you to wait on a windows events. WaitOne is non-static and waits on the event handle for that object. WaitAll and WaitAny are static class-wide methods that wait an a group of event handlers. WaitAll waits for all events to signal and WaitAny for a single event in the group.

kenny
Not sure what you mean by 'wait on windows events'. WaitOne is primarily used to synchronise asynchronous operations i.e one thread synchronising with another thread. The OP was asking about AsyncWaitHandle.WaitOne. AsyncWaitHandle is a WaitHandle typically used to synchronise a BeginInvoke i.e it removes the need for to poll and call IsCompleted on the IAsyncResult
zebrabox
It can also be used for windows events (e.g. CreateEvent) see SafeWaitHandle property and http://msdn.microsoft.com/en-us/library/microsoft.win32.safehandles.safewaithandle.safewaithandle.aspx.
kenny
+10  A: 
  1. WaitHandle.WaitOne() is an instance method that will block until the wait handle is signalled (when the operation is completed). If it has already been signalled previously (i.e. operation already completed) it is possible that the call to WaitOne() will not block.
  2. WaitHandle.WaitAll() and WaitHandle.WaitAny() are static methods that allow you to specify a number of wait handles to monitor simultaneously:

    • WaitHandle.WaitAll() will wait until all of the specified handles have been signalled before it returns.
    • WaitHandle.WaitAny() will return as soon as any of the specified handles have been signalled.

    These methods allow you to monitor a number of outstanding asynchronous operations at the same time.

Background: AsyncWaitHandle is the name of a property on the IAsyncResult interface. The actual type of this property is WaitHandle, which is an abstract base class for types such as Semaphore, Mutex, and Manual/AutoResetEvent.

See also: Blocking Application Execution Using an AsyncWaitHandle (MSDN)

Daniel Fortunov
Before EndInvoke() do we need to call them?
You're right but I think the OP was asking specifically about AsyncWaitHandle.WaitOne(). So I think he was asking whether the call to WaitOne would always block even if the async op had completed, which of course it wouldn't
zebrabox
No. EndInvoke() may be called any time after BeginInvoke() and will block if the operation has not completed yet.
Daniel Fortunov
@threadpool.Calling EndInvoke will block until the async op has completed so you don't need to call WaitOne if you don't wish too. However if you do call WaitOne you still need to call EndInvoke, especially if your async op returns a val etc
zebrabox
And you should always call EndInvoke otherwise you'll leak resources (the wait handles used internally).
Will
Your second bullet under number 2 should be WaitHandle.WaitAny(), not WaitOne().
Mike Pateras
note: WaitAll() can only manage up to 64 WaitHandles
Mauricio Scheffer
@Mike Fixed, thanks for pointing that out.
Daniel Fortunov