views:

38

answers:

2

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?

A: 

You could replace the call to AcceptTcpClient with one for Socket.Select(), which can timeout.

var sockl = new ArrayList { listener.Server };
Socket.Select(sockl, null, null, _timeout_);
if (sockl.Contains(listener.Server)) listener.AcceptTcpClient();
Mark H
+1  A: 

Simple solution. CHECK with pending.

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