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.
source share