Search code examples
.netsocketsmultithreadingtcplistener

TcpListener Timeout/about/something? Without ASync?


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?


Solution

  • Simple solution. CHECK with pending.

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