473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I cancel a BeginReceiveFrom ?

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
Nov 16 '05 #1
6 6294
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.

Nov 16 '05 #2
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

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:u9****************@cpmsftngxa06.phx.gbl...
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.

Nov 16 '05 #3
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.

Nov 16 '05 #4
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.

Nov 16 '05 #5
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

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:S3**************@cpmsftngxa10.phx.gbl...
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.

Nov 16 '05 #6
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.

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: AP | last post by:
Hi, I'm trying to use c# to pop up a dialog box when a user attempts to close word to prompt them if they want to exit or cancel (obviously other stuff needs to happen based on their selection...
2
by: Jim H | last post by:
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....
13
by: Mike L | last post by:
I have a child form frmDataEntry call up another child form frmDealerSearch. If the user clicks on cancel on frmDealerSearch, I want to close frmDealerSearch and put the focus on txtDealerNum on...
14
by: clintonG | last post by:
This is an appeal for peer support sent to Microsoft as will be noted in closing. The Login control does not include a Cancel button. The only option is to convert the Login control to a...
3
by: Charles Law | last post by:
Under what circumstances would e.Cancel be set to True on entry to the Closing event of an MDI child form? I have found that this is why my application won't close properly. I can explicitly set...
0
by: J008 | last post by:
Just looking for some insight as to why the callback "BeginReceiveFromCallback" is not being called in my "Receive" Subroutine below (when I call BeginReceiveFrom). I am trying to read data...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I change the confirm box to say yes/no or default to cancel?...
16
by: parez | last post by:
I start a BackGroundWorker to populate a grid. It is started off in the ui layer The thread follows( cannot think of a better word) the path UI->Layer1->Layer2->Communication Layer and it...
5
by: ghjk | last post by:
I have "cancel" button in php files. I want to write common javascript function for cancel button. When user click cancel button I want to clear php form data. Is it possible? <input id="Cancel"...
0
kdsdata
by: kdsdata | last post by:
Introduction There are a number of (good) examples of how to cancel via forms, controls and msgbox(s). However, there is often a need to execute a cancel that carries up through calling subs. Here...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.