The right way to break a locked thread

I am creating a server with a TCP connection. A TCP connection is made in its thread for an indefinite period of time. Is there a good template for safely disabling TcpListener and Client, as well as the stream? Below I still know.

private volatile bool Shudown; void ThreadStart1() { TcpListener listener = null; TcpClient client = null; Stream s = null; try { listener = new TcpListener(60000); client = listener.AcceptTcpClient(); Stream s = client.GetStrea(); while(!Shutdown) // use shutdown to gracefully shutdown thread. { try { string msg = s.ReadLine(); // This blocks the thread so setting shutdown = true will never occur unless a client sends a message. DoSomething(msg); } catch(IOException ex){ } // I would like to avoid using Exceptions for flow control catch(Exception ex) { throw; } } } catch(Exception ex) { LogException(ex); throw ex; } finally { if(listener != null) listener.Close(); if(s != null) s.Close(); if(client != null) client.Close(); } } 
+4
source share
3 answers

Is there a good template for safely disconnecting a stream?

Change the while loop to the following:

 while (!interrupted){ // Do something } // declare interrupted as volatile boolean volatile bool interrupted; 

See the MSDN example for more details. Setting an interrupt with a boolean value to true will cause the thread to exit the loop when it checks the while condition.

Is there a good template to safely disable TcpListener and Client?

To avoid duplication check this SO question

Regarding the question of how to interrupt a blocking thread on ReadLine(); , next listener.Server.Close(); must complete the task and return from the blocking call.

+2
source

Set a timeout in NetworkStream (client.ReadTimeout = ...). Once the read operation is complete, make sure that the main thread signals you to stop (by setting a variable or AutoResetEvent). If he had been warned of termination, exit gracefully. If not, try again until the next timeout.

Setting a timeout of 0.5 or 1 second should be sufficient - you can exit the stream in a timely manner, but still be very easy on the CPU.

+5
source

Perhaps instead of calling the Read synchronously on the NetworkStream, you should use BeginRead and EndRead to do this asynchronously, and call Close () on NetworkStream when you are done with it.

+1
source

Source: https://habr.com/ru/post/1383272/


All Articles