473,383 Members | 1,795 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,383 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 6304
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.