I have this code in a separate thread (Task) that runs first when the application starts and should not end until the application closes:
TcpListener tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
while (true)
{
TcpClient client = tcpListener.AcceptTcpClient();
Task.Factory.StartNew(HandleClientCommunication, client);
}
In this case is it necessary to call tcpListener.Stop()
? This thread runs for the entire duration of the application and if I did need to call it, where would I do so? The listener is local to this thread. Instead of having a while (true)
loop would I have a while (appRunning)
loop and set appRunning to false in the FormClosing event? Then after the while loop I could call tcpListener.Stop()
.
However, is it even necessary to call TcpListener.Stop()
because the application has already closed at that point and since I'm using Tasks the process ends as well?