Le richiamate sono indipendenti in quanto vengono invocate sui worker di completamento degli ordini dei pool di thread.
Se sei interessato, puoi vederlo nello source code. Questo particolare metodo è per la classe Socket
(che TcpClient
e UdpClient
uso interno), dove si sovrapponevano IO viene utilizzato per richiamare il callback (si veda il commento in cima asyncResult.SetUnmanagedStructures
invocazione:
private void DoBeginReceiveFrom(byte[] buffer, int offset,
int size, SocketFlags socketFlags,
EndPoint endPointSnapshot, SocketAddress
socketAddress, OverlappedAsyncResult asyncResult)
{
EndPoint oldEndPoint = m_RightEndPoint;
SocketError errorCode = SocketError.SocketError;
try
{
// Set up asyncResult for overlapped WSARecvFrom.
// This call will use completion ports on WinNT and Overlapped IO on Win9x.
asyncResult.SetUnmanagedStructures(
buffer, offset, size,
socketAddress, true /* pin remoteEP*/,
ref Caches.ReceiveOverlappedCache);
asyncResult.SocketAddressOriginal = endPointSnapshot.Serialize();
if (m_RightEndPoint == null)
{
m_RightEndPoint = endPointSnapshot;
}
int bytesTransferred;
errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecvFrom(
m_Handle,
ref asyncResult.m_SingleBuffer,
1,
out bytesTransferred,
ref socketFlags,
asyncResult.GetSocketAddressPtr(),
asyncResult.GetSocketAddressSizePtr(),
asyncResult.OverlappedHandle,
IntPtr.Zero);
if (errorCode!=SocketError.Success)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
}
}
catch (ObjectDisposedException)
{
m_RightEndPoint = oldEndPoint;
throw;
}
finally
{
errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
}
}
fonte
2015-06-22 13:10:38
Quale classe stai usando a 'BeginReceive 'on? –
@Yuval - La classe utilizzata per le chiamate Async e Sync è Socket. – Robert