Something isn't clear (to me) in the documentation.
In the "Remarks" section for the Socket.BeginReceive method, it says:
You can create a callback method that implements the AsyncCallback
delegate and pass its name to the BeginReceive method.
Does this "can" imply that the use of a callback is optional?
If no callback is specified, can you also omit the State parameter from the
BeginReceive call? (just pass Nothing or NULL for the last two parameters)
Basically, I would want use a worker thread that initializes a read on a
socket, and then uses WaitHandle.WaitAny on an array of handles, one of
which corresponds to the read operation.
Not all handles will correspond to sockets, each of them just signals some
action the worker thread has to do.
It seems to me that with a single WaitAny, my code would become simpler and
I'd save the extra thread the runtime uses for the callback.
Lucvdv - 17 Aug 2004 13:21 GMT
> Something isn't clear (to me) in the documentation.
> In the "Remarks" section for the Socket.BeginReceive method, it says:
[quoted text clipped - 3 lines]
>
> Does this "can" imply that the use of a callback is optional?
It compiles and runs, but [my code?] doesn't work.
Related question: also in the documentation,
The Connected property gets the connection state of the Socket as of
the last I/O operation. When it returns false, the Socket was either
never connected, or is no longer connected.
Could it be that this is true only for connection state changes made
locally, or that it goes wrong after BeginRead has been called without a
callback?
If the connection (TCP, stream) is closed by the other side, Connected
remains True indefinitely. Read attempts keep returning immediately with 0
bytes read, but without any indication of an error and without adjusting
the state of the Connected property.
I'm including a VB code snippet, running in a worker thread: after the
connection has been closed by the other side, it just keeps printing
"Empty" until the thread is killed.
ar = m_Socket.BeginReceive(RecvData, 0, RecvData.GetUpperBound(0), _
SocketFlags.None, Nothing, Nothing)
WaitHandles = New WaitHandle() {ar.AsyncWaitHandle, [...]}
Do
Select Case WaitHandle.WaitAny(WaitHandles, 100, False)
Case 0 ' Data received
If m_Socket.Connected Then
Dim Bytes As Integer = m_Socket.EndReceive(ar)
If Bytes > 0 Then
Dim s As String = Encoding.ASCII.GetString(RecvData, _
0, Bytes)
Debug.WriteLine(Bytes & ";" & s)
[...]
Else
Debug.WriteLine("Empty")
End If
[restart BeginReceive & re-initialize WaitHandles array]
End If
[Other cases]
End Select
Loop While m_Socket.Connected
Eugene Mayevski - 17 Aug 2004 17:56 GMT
Hello!
You wrote on Tue, 17 Aug 2004 11:04:10 +0200:
L> Basically, I would want use a worker thread that initializes a read on a
L> socket, and then uses WaitHandle.WaitAny on an array of handles, one of
L> which corresponds to the read operation.
What would you use as a WaitHandle? You can use Select method on a socket
and this will work (tried just yesterday).
With best regards,
Eugene Mayevski
Lucvdv - 19 Aug 2004 12:05 GMT
> Hello!
> You wrote on Tue, 17 Aug 2004 11:04:10 +0200:
[quoted text clipped - 4 lines]
>
> What would you use as a WaitHandle?
The AsyncWaitHandle member of the IASyncResult returned by BeginReceive.
WaitAny is a static/shared member of WaitHandle in case that's what you
meant, you don't need a separate instance.
This is aproximately how my VB code is structured:
Try
WaitHandles = New WaitHandle() {WaitHandle1, WaitHandle2}
Do
Select Case WaitHandle.WaitAny(WaitHandles, msecTimeOut)
Case 0:
' WaitHandle1 got signaled
Case 1:
' WaitHandle2 got signaled
Case WaitTimeout:
' Some background processing, if necessary
End Select
Loop
Catch ex as ThreadAbortException
' We're being asked to terminate
' The only way the endless loop ever gets broken
End Try
> You can use Select method on a socket
> and this will work (tried just yesterday).
Only one of the handles I'm waiting for belongs to a socket, another
indicates a certain action has been requested by the UI thread, etc.