TcpListener Timeout/about/something? Without ASync?

user34537 picture user34537 · Jul 22, 2010 · Viewed 8.5k times · Source

I create a thread which uses TcpListener and when my app closes i'd like the thead to terminate. I can call abort but the thread is still alive since TcpListener is blocking with AcceptTcpClient.

Is it possible to about or set a timeout or to do SOMETHING with AcceptTcpClient? i cant imagine how it would be useful if theres no way to stop it from blocking forever. My code is serial and i would like it to stay that way so is there a solution without using BeginAcceptTcpClient? and writing ASync code?

Answer

user34537 picture user34537 · Jul 23, 2010

Simple solution. CHECK with pending.

while(!server.Pending())
{
    Thread.Sleep(10);
}
TcpClient client = server.AcceptTcpClient();