How do I cancel a BeginReceiveFrom ?

  • Thread starter Thread starter Jim H
  • Start date Start date
J

Jim H

I'm sure this has been asked a million times, but I did a search and didn't
see anything.
I have a socket in my form and I call MySocket.BeginReceiveFrom to listen
for incoming data asyncronously. How do I cancel it so I can close the
application. I call MySocket.Shutdown(SocketShutdoen.Both) then
MySocket.Close in the Form.Dispose method. I get an error when the app
closes in the OnReceive async callback when it tries to call
MySocket.ReceiveFrom.

There has to be a way to cancel the read or at least wait for the
EndReceiveFrom. The OnReceive callback should get triggered by the socket
shutdown right?

Thanks,
jim
 
Hi Jim,

Based on my understanding, you want to close your reading side socket and
then exit your application.

Can not you just invoke Socket.Close method to close the socket and then
close your winform application?

What exception does your code generate when closing your application?

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks Jeff (Jeffrey?)

Doing a Shutdown(BOTH) closes the read and write side of the socket. Is
there another way? I also call Close() right after Shutdown(). I do both
of these in the Form.Dispose method (before the call to base.Dispose).

The exception:
Additional information: Cannot access a disposed object named
"System.Net.Sockets.Socket".

The Socket is Disposed before the Receive thread is finished executing. I
don't know how to check for that? I think just catching that exception is
poor work around. I'd rather do it right.

jim
 
Hi Jim,

Thanks very much for your feedback.

Oh, yes, based on your exception message, it means that your main thread
closed the socket object, while the receive thread is still using it, so
this exception will generate.

This is an expected behavior, if you search "Cannot access a disposed
object" and "Socket" in groups.google.com, you will see that there are a
lot of such problems when using asynchronize socket operation.

There are 2 ways to workaround this:

1. Setup a flag for your receive thread. When your main thread is going to
close the socket, it sets this flag, then your receive thread may detect
this and exits.(You should be careful of the synchronization and mutex)

2. Just catch this exception and terminate properly.(Which you do not like)

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jim,

Have you tried my suggestion? Do you still have any concern on this issue?

Please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
I used a "ClosingFlag" variable and check it from the OnReceive event. I
thought there was a more built in way to cancel the BeginReceiveFrom method
but I guess not. The flag works fine and it's just a test client for
testing a server anyway. When I write an actual client peice I'll do it
more cleanly.

Thanks again,
jim
 
Hi Jim,

Thanks very much for your feedback.

I am glad my reply makes sense to you. Yes, there is not a build-in way for
handling this issue, but because it is general issue, there are some common
ways to handle it.

Also, I want to inform you that, because your ClosingFlag is crossing
threads, you should take care of the mutex problem(As I mentioned in last
reply), in .Net, there are some convinient ways to implement mutex, please
refer to: System.Threading.Interlocked class and System.Threading.Mutex
class.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top