看板 C_Sharp 關於我們 聯絡資訊
最近寫程式遇到一個怪問題,想請教一下 我有一個 thread 裡面有一個 tcp listener,可是這個 thread 卻不會結束 所以每次這個程式關閉之後,它的 process 還是在 我試著在 class 的 deconstructor 裡面使用 abort() 但是這個 thread 還是不會結束,我是參考 MSDN 的範例寫的 http://tinyurl.com/6dcx6nb 我自己的程式碼如下,可以請問一下可能是哪邊出了問題嘛? (我使用的是 visual studio express 2008, windows 7) public class TcpServer { Thread TcpThread; IPAddress localhost; public TcpServer(){ localhost = Dns.Resolve("localhost").AddressList[0]; TcpThread = new Thread(Server); TcpThread.Start(); } ~TcpServer(){ if (TcpThread.isAlive == true){ TcpThread.Abort(); } } void Server(){ TcpListener listener = new TcpListener(localhost, 1111); listener.Start(); try{ while(true){ using (TcpClient c = listener.AcceptTcpClient()) using (NetworkStream n = c.GetStream()){ //some process } } } catch(Exception Exp){ } finally{ listener.Stop(); } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 72.130.134.2
phantasma:第一個提供正解的人我會送上 500p 幣作為答謝 09/29 17:13
※ 編輯: phantasma 來自: 72.130.134.2 (09/29 17:15)
iterator:TcpThread.IsBackground = true; 09/29 17:45
phantasma:感謝樓上幫忙,p幣已經奉上~ 09/29 18:14