PC Review


Reply
Thread Tools Rate Thread

ArgumentException: IAsyncResult object was not returned from the corresponding...

 
 
Tester
Guest
Posts: n/a
 
      9th Jan 2004
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



 
Reply With Quote
 
 
 
 
Tom Hall
Guest
Posts: n/a
 
      9th Jan 2004
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


"Tester" <test> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Tester
Guest
Posts: n/a
 
      9th Jan 2004
I think you're right. Thanks for pointing me in the right direction. I'll
try it the recommended way

Thanks a lot

"Tom Hall" <(E-Mail Removed)> wrote in message
news:Oc9$$(E-Mail Removed)...
> 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
>
>
> "Tester" <test> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >
> >

>
>




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# CFC Issue - The IASyncResult object was not returned from the corresponding asynchronous method on this class Yann STEPHAN via .NET 247 Microsoft Dot NET Compact Framework 2 16th Apr 2010 06:07 PM
Returning an IAsyncResult object? deostroll Microsoft Dot NET Framework 6 27th Jan 2009 04:19 PM
How is IAsyncResult returned from HttpWebRequest.BeginGetResponsesupposed to be used? Brad Wood Microsoft Dot NET Framework 6 3rd Jan 2006 05:45 PM
IAsyncResult & 'Object Reference Not set to an Instance of...' mail.matty@gmail.com Microsoft C# .NET 1 16th Aug 2005 06:01 PM
Casting returned object Stuart Miles Microsoft C# .NET 3 3rd Mar 2005 05:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:23 PM.