如何把在一个线程中的TcpListener释放?比如,在某个线程中的程序是这样:tcpListener.Start();while (true) { tcpClient = tcpListener.AcceptTcpClient(); //线程释放的时候,能保证程序一定停在这一句话等待。那么问题是怎么同时把tcpListener也释放了? ......}不好意思,刚刚说错了,问题是程序停在那句话上面了。该怎么才能把这线程结束了。
对于不能直接Dispose的资源,比如IO等,需要在停止线程前现行关闭。解决办法如下:tcpListener.Start();while (true) {tcpClient = tcpListener.AcceptTcpClient(); //线程释放的时候,能保证程序一定停在这一句话等待。那么问题是怎么同时把tcpListener也释放了?......}建议修改为public class worker{public TcpListener tcpListener;public void work(){this.tcpListener.Start();while (true) {tcpClient = tcpListener.AcceptTcpClient(); //线程释放的时候,能保证程序一定停在这一句话等待。那么问题是怎么同时把tcpListener也释放了?......}}}启动线程的代码worker wk = new worker();Thread th = new Thread(new ThreadStart(wk.work));th.Start()停止线程的代码wk.tcpListener.Stop();th.Abort();