I think I know what's going on. Since you didn't show much code, I assume
you are holding the socket object in a class-scoped variable. So, if
somehow the socket gets disconnected while the ReadCallback is executing and
you reconnect it (thereby changing the socket object) - now they don't
match, you are trying to complete the Async request using the new object
when the old one is the one actually completing. This is why all the
examples in MSDN tell you to pass your socket as the "state" parameter - not
because it must be passed that way only (although they say it as if it were
so) but because doing it their way ensures you are always attempting to call
EndReceive on the correct socket corresponding to the IAsyncResult object!
You also must get real unlucky and have a thread context switch at just the
right moment occasionally which allows the newly connected socket to
overwrite your old one - between the start of the callback and when your
callback code calls EndReceive.
Hope this helps
Tom
[quoted text, click to view] "Tester" <test> wrote in message
news:Os0ZNCm1DHA.1508@TK2MSFTNGP12.phx.gbl...
> Hello,
>
> Could someone tell me under what conditions the following exception occurs
> with async sockets:
>
> "System.ArgumentException: The IAsyncResult object was not returned from
the
> corresponding synchronous method on this class."
>
> This is being thrown from within the callback that is specified to
> Socket.BeginReceive (happens rarely, but still does happen)
>
> I see only 1 reference to this exception on the whole web, which is not
> answered either.
>
> The steps are very simple,
>
> start()
> {
> Socket.BeginReceive(ReadCallback)
> }
> ReadCallback()
> {
> Socket.EndReceive
> Socket.BeginReceive
> }
>
> One case that i noticed where this happens is when code is still executing
> inside ReadCallback and socket gets disconnected then reconnected and
> another Socket.BeginReceive is scheduled while previous ReadCallback
didn't
> return yet. There are some other cases where this exception occurs.
>
> Thanks for any input on this
>
>
>