473,414 Members | 1,951 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,414 software developers and data experts.

thread not closing

2
Hi, as the title suggests, I am having trouble making a thread close properly. I'll give a brief overview and then post relevent code. I have a server that needs to handle connections from multiple clients. Currently I can get as many clients connected as I want, but it is the disconnect that is not working and the crash only happens in a certain order:

Example 1:
1. Client A connects to Server
2. Client B connects to Server
3. A disconnects from server causing crash

or this order:

Example 2:
1. Client A connects to Server
2. Client B connects to Server
3. B disconnects from server *no Crash*
4. A disconnects causing crash

A little more information now. When only one client is connected and decides to disconnect, the thread.stop() properly closes the thread for that connection. actually now that I think of it, when example 2 takes place. Client A works perfectly fine till he decides to disconnect, then the server crashes. The crash seems to occur when the thread begins the loop again and tries to read a closed streamreader. Keep in mind that when the disconnect is called it not only calls a stop on the Thread but also shuts down the stream. So the IOException makes perfect sense. What doesn't is that the clients must seemingly disconnect in reverse order to avoid the crash, which is obviously unacceptable. If you spent enough time to read all of that, i thank you. Now comes the code.

Expand|Select|Wrap|Line Numbers
  1. static void CommandFunction()
  2.         {
  3.             NetworkStream ns = new NetworkStream(client);
  4.             StreamReader sr = new StreamReader(ns);
  5.             StreamWriter sw = new StreamWriter(ns);
  6.             while (true)
  7.             {
  8.                 string data;
  9.                 IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
  10.                 data = "";
  11.                 data = sr.ReadLine();
  12.                 String message = data;
  13.                 switch (message)
  14.                 {
  15.                 case "server_quit":
  16.                         {
  17.                             Console.WriteLine("Disconnecting With {0} ", newclient.Address);
  18.                             ns.Close();
  19.                             sr.Close();
  20.                             sw.Close();
  21.                             Console.WriteLine("Connection Terminated");
  22.                             CommandThread.Abort();
  23.                             break;
  24.                         }
  25.                 }
  26.             }
  27.         }
I am only posting the one function because I don't want to confuse anyone since this is the only relevant block of code. Thanks in advance.
Nov 6 '06 #1
1 1294
bioxx
2
I had a feeling this would happen. I type all of that out and a short time later it dawns on me what to do. I moved the code to close the streams from the server, to the client and now it disconnects in any order, perfectly fine. Oh well, problems are often solved this way.
Nov 7 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
4
by: Dr. J | last post by:
How to terminate a blocked thread? In my form's "load" I launch a TCP listening thread that stays in an infinite loop waiting for incoming TCP packets. In this form's "closing" I try to...
5
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
3
by: Sagaert Johan | last post by:
Hi If i have a form application that creates a thread, is there a way to detect from within that thread if i have closed the form ? Now i use the forms closing event to end the thread i...
7
by: Robert Smith | last post by:
Hello I wish to execute some code when and only when a thread has completed like so thrRestore = New Thread(AddressOf RestoreDb) thrRestore.IsBackground = True thrRestore.Start() ??? I only...
9
by: Brett | last post by:
I'm trying to kill a thread spawned this way: Form1 spawns Class1 via Thread.start() Here's my code to kill the thread: If (t.ThreadState.ToString = "SuspendedRequested, WaitSleepJoin") Or...
5
by: taylorjonl | last post by:
I am completely baffled. I am writting a daemon application for my work to save me some time. The application works fine at my home but won't work right here at work. Basically I have a...
2
by: Mike | last post by:
Hello, Ok I have 2 classes in my project, one is the main form and one is a connection class, at a certain event on my main form a new instance is made of the connection class, and a reference...
1
by: priyamtheone | last post by:
Is there any way possible in a windows application project to kill the default current thread of the project and run a new thread and then relate the project to it? My case scenario:- I...
4
Fr33dan
by: Fr33dan | last post by:
Hi, I'm having trouble with a multi-threaded program crashing on a specific machine when the worker thread is not initialized at when the _FormClosing method of my main form called. Here is my...
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
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...

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.