Why can't I use WaitHandle from Socket.BeginSend to wait for the operation to complete?

I used the following code to send data asynchronously, but I noticed that using WaitOne in AsyncWaitHandle, which I get from asyncRes , does not wait at all. I checked the MSDN and said that I should use ManualResetEvent.

...
var asyncRes = _socket.BeginSend(encodedFrame, 0, encodedFrame.Length, SocketFlags.None, sendCallback, _socket);
...
var success = asyncRes.AsyncWaitHandle.WaitOne(_timeout, true);
...


 private void sendCallback(IAsyncResult ar)
 {
     _socket.EndSend(ar);            
 }

MSDN also says in IAsyncResult:

AsyncWaitHandle: returns WaitHandle, which is used to wait for the completion of the asynchronous operation.

So why can't I use it for this purpose?

Thank.

+3
source share
3 answers

, . . BeginSend " , ". . , . , - .

FileStream.BeginWrite(), . , , .

Anyhoo, WaitOne() . . API-, , . , , Windows .

+3

true , . false, .

true, "".

+1

?

/ async , , , , , , .

var ar = _socket.BeginSend(encodedFrame, 0, encodedFrame.Length, SocketFlags.None);
//...
_socket.EndSend(ar); // this is a blocking operation

async IO, ...

+1
source

Source: https://habr.com/ru/post/1769880/


All Articles