Can someone tell my why when I call Socket.BeginReceive() the callback is
called on the same thread? I thought that BeginReceive was ALWAYS an async
method. Below is the call stack. You can see its calling my callback
"Connection()" from the same thread.

My problem is that I call Accept() then BeginReceive(). If its processed on
the same thread then other connections will not be accepted. I could pass
it to another thread myself but shouldn't BeginReceive work this way?



MyApp.exe!MyApp.MStore.Connection(System.IAsyncResult iar =
{System.Net.Sockets.OverlappedAsyncResult}) Line 416 + 0x32 bytes C#

System.dll!System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) +
0x7f bytes

System.dll!System.Net.ContextAwareResult.CaptureOrComplete(ref
System.Threading.ExecutionContext cachedContext, bool returnContext) + 0x12b
bytes

System.dll!System.Net.ContextAwareResult.FinishPostingAsyncOp(ref
System.Net.CallbackClosure closure = null) + 0x69 bytes

System.dll!System.Net.Sockets.Socket.BeginReceive(byte[] buffer, int offset,
int size, System.Net.Sockets.SocketFlags socketFlags, out
System.Net.Sockets.SocketError errorCode, System.AsyncCallback callback,
object state) + 0x14d bytes

System.dll!System.Net.Sockets.Socket.BeginReceive(byte[] buffer, int offset,
int size, System.Net.Sockets.SocketFlags socketFlags, System.AsyncCallback
callback, object state) + 0x23 bytes

MyApp.exe!MyApp.MStore.WorkerThread() Line 625 + 0x38 bytes C#

Re: BeginReceive not ASYNC. by Vadym

Vadym
Fri Jan 13 12:00:07 CST 2006

> My problem is that I call Accept() then BeginReceive(). If its processed
> on the same thread then other connections will not be accepted. I could
> pass it to another thread myself but shouldn't BeginReceive work this way?

The async request may complete synchronously, this comes from the internal
GetOverlappedResult function
of the kernel32.dll

if GetOverlappedResult call sets the result to WSA_IO_PENDING - then new
thread is spawned and you wait for operation completion.
if result is 0 ( no error ) operation is considered to be complete - the
same thread is used to process the callback
( you should check IAsyncResult.CompletedSynchronously to define this )
all the other results indicate that error happened.

Generally, if your callback takes much time to execute you can put the work
there on the separate thread, but this must be done only when
IAsyncResult.CompletedSynchronously is equal to true.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Bob" <msgdev@hotmail.com> wrote in message
news:OFSAu%23FGGHA.2708@TK2MSFTNGP11.phx.gbl...
> Can someone tell my why when I call Socket.BeginReceive() the callback is
> called on the same thread? I thought that BeginReceive was ALWAYS an
> async method. Below is the call stack. You can see its calling my
> callback "Connection()" from the same thread.
>
> My problem is that I call Accept() then BeginReceive(). If its processed
> on the same thread then other connections will not be accepted. I could
> pass it to another thread myself but shouldn't BeginReceive work this way?
>
>
>
> MyApp.exe!MyApp.MStore.Connection(System.IAsyncResult iar =
> {System.Net.Sockets.OverlappedAsyncResult}) Line 416 + 0x32 bytes C#
>
> System.dll!System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) +
> 0x7f bytes
>
> System.dll!System.Net.ContextAwareResult.CaptureOrComplete(ref
> System.Threading.ExecutionContext cachedContext, bool returnContext) +
> 0x12b bytes
>
> System.dll!System.Net.ContextAwareResult.FinishPostingAsyncOp(ref
> System.Net.CallbackClosure closure = null) + 0x69 bytes
>
> System.dll!System.Net.Sockets.Socket.BeginReceive(byte[] buffer, int
> offset, int size, System.Net.Sockets.SocketFlags socketFlags, out
> System.Net.Sockets.SocketError errorCode, System.AsyncCallback callback,
> object state) + 0x14d bytes
>
> System.dll!System.Net.Sockets.Socket.BeginReceive(byte[] buffer, int
> offset, int size, System.Net.Sockets.SocketFlags socketFlags,
> System.AsyncCallback callback, object state) + 0x23 bytes
>
> MyApp.exe!MyApp.MStore.WorkerThread() Line 625 + 0x38 bytes C#
>
>
>