472,330 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Stopping Thread Blocking on Socket.Receive

How can I cleanly stop a thread that is currently blocking on
Socket.Receive?

I don't want to use Thread.Abort, because I would like the thread
method to exit cleanly, and the same code must run under the Compact
Framework, which does not support Abort.

Will Socket.Close cause the Receive method to finish, or is there a
better way?

--Bruce
Nov 15 '05 #1
2 12355
Just tossing some ideas on the wall. Maybe create your socket (or use
tcpclient/tcpserver) and set a ReceiveTimeout (in ms). Next line would be a
test for a "cancelled" bool in your class. If "cancelled" then break out,
if not, then spin back to socket.receive. Another way may be to use
out-of-band data to the socket as a signal.

--
William Stacey, DNS MVP

"Bruce Vander Werf" <br*****@hotmail.com> wrote in message
news:pa********************************@4ax.com...
How can I cleanly stop a thread that is currently blocking on
Socket.Receive?

I don't want to use Thread.Abort, because I would like the thread
method to exit cleanly, and the same code must run under the Compact
Framework, which does not support Abort.

Will Socket.Close cause the Receive method to finish, or is there a
better way?

--Bruce

Nov 15 '05 #2
Bruce Vander Werf <br*****@hotmail.com> wrote in message news:<pa********************************@4ax.com>. ..
How can I cleanly stop a thread that is currently blocking on
Socket.Receive?

I don't want to use Thread.Abort, because I would like the thread
method to exit cleanly, and the same code must run under the Compact
Framework, which does not support Abort.

Will Socket.Close cause the Receive method to finish, or is there a
better way?

Bruce -

If you do not (or can not) abort the thread, the only other
options you have is to either make the Receive() method stop blocking,
use an Asynchronous Receive() method, or use the Poll() or Select()
methods to check the socket before performing the Receive().

There are a couple of ways to make a blocking Receive() method
stop blocking. As suggested by William, you can set the ReceiveTimeout
property of the Socket, forcing the socket to throw an Exception after
a set timeout value. Alternatively, you can create the Socket in the
main Thread and pass that object to the new Thread object. While the
new Thread is blocking on the Receive() method, the main thread can
call the Close() method. This will force the Receive() method to throw
an Exception, which of course you should handle accordingly to cleanly
exit the thread.

Hope this gives you some more ideas to work with. Good luck.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Nov 15 '05 #3

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

Similar topics

12
by: Sven Groot | last post by:
I have a Windows Service application that acts as if it's an SMTP server. Outlook connects to this service, which is always running on the...
4
by: Joe Kinsella | last post by:
The following code behaves differently from what I would expect: socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,...
6
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet...
4
by: Alexander Muylaert | last post by:
Hi I their a way I can interupt socket.Receive. I want my multi-threader server to be able to handle requests as wel as Broadcast messages. ...
0
by: yaron | last post by:
Hi, I want to use non-blocking socket operations (and not async socket operations) When i use this code : socket.Blocking = false;...
9
by: yaron | last post by:
Hi, 1. It seems to me that there is a bug when using non-blocking socket connect operation, because the LocalEndPoint, RemoteEndPoint of the...
2
by: Qindong Zhang | last post by:
My socket application blocked at socket.receiver() after received all information from sender. Should socket.Receive() return 0 after no more data...
4
by: Rollasoc | last post by:
Hi, We have a range of four products that can talk to our software (Written in C# & managed C++) via ethernet. Using standard socket class. ...
0
by: chsalvia | last post by:
On Linux (Ubuntu) I'm trying to figure out how to avoid recv() calls that hang forever by using using a non-blocking socket along with the select()...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.