473,508 Members | 4,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

shutting down a socketreceiving thread

Hello,

I have following thread, that reads data from a socket.

private void RecThread()
{
int Len = 0;
byte[] input = new byte[1024];

while((Len = socket.Receive(input)) > 0) //*1
{
...
}
}

If I simply close the socket, I get an exception at *1
And I wont Abort() the thread.
How can I close the connection in a "clean" way?
Nov 16 '05 #1
2 1068
one way to do it is to create a global flag that the worker thread
check can check as it busy loops. if the flag is set, it breaks from
the loop.

"Dirk Reske" <_F*******@gmx.net> wrote in message news:<O$**************@TK2MSFTNGP10.phx.gbl>...
Hello,

I have following thread, that reads data from a socket.

private void RecThread()
{
int Len = 0;
byte[] input = new byte[1024];

while((Len = socket.Receive(input)) > 0) //*1
{
...
}
}

If I simply close the socket, I get an exception at *1
And I wont Abort() the thread.
How can I close the connection in a "clean" way?

Nov 16 '05 #2
Catch the SocketException and quit. AFAICT, you must close the socket to
break from the blocking call. Another flag, does not do that after you are
already blocking on receive. However, you should probably loop flag
condition, that way you always check before blocking on receive again.
while ( ! stopped )
{
try
{
len = socket.Receive(input);
// etc..
}
catch( SocketException se) { break; }
}

--
William Stacey, MVP

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:O$**************@TK2MSFTNGP10.phx.gbl...
Hello,

I have following thread, that reads data from a socket.

private void RecThread()
{
int Len = 0;
byte[] input = new byte[1024];

while((Len = socket.Receive(input)) > 0) //*1
{
...
}
}

If I simply close the socket, I get an exception at *1
And I wont Abort() the thread.
How can I close the connection in a "clean" way?


Nov 16 '05 #3

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

Similar topics

10
18618
by: Jesper | last post by:
Does anyone know some c++ code for shutting down windows? I want to write a program using borland c++ builder to shut down windows. --
4
2205
by: sanket gupta | last post by:
Hi guys , I face a peculiar problem. I have a mutithreaded software trhat uses sockets to connect to my webserver. The problem is that while my software is running , windows is unable to...
1
2053
by: mikelostcause | last post by:
I'm working on shutting down an app that runs in the system tray, I have no problems shutting down, but I have problems saving data first. if the base.WndProc(ref m) is placed at the top, it...
4
3396
by: Markus Stoeger | last post by:
Hi, I have a problem with Application.Run() when Windows is shutting down. Please have a look at the copy&paste example program below. The application has no forms. It has only got a notify...
7
4249
by: Mark | last post by:
It is possible from a .NET application to prevent Windows from shutting down? I understand that a .NET application can "know" that windows is initiating the process of to shutting down - but...
3
3169
by: leocwh | last post by:
Dear all, I would like to know how to run the execuatable before windows shutting down. Here is my simple code: Private Sub Command1_Click() Shell "C:\abc.bat", vbNormalFocus End Sub ...
2
13649
by: Jack | last post by:
Sorry for the double post (also in the IIS group). We've got an ASP.Net 2.0 app running on IIS6. We kept losing sessions, and enabled health monitoring to see what was happening. This morning...
4
3687
by: Larry Bud | last post by:
Not sure where else to post this.... One of our developers quit and I'm taking over stuff from him. He had a COM app that would lock files on occasion (yeah, thanks for leaving that in...
2
2007
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am monitoring web apps with the windows event log and noticed several (Hosting environment is shutting down) statements. The server appears to still be running, just wondering any ideas on...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.