[問題] 無法中斷的 thread

看板C_Sharp (C#)作者 (PeterPan)時間14年前 (2011/09/29 17:11), 編輯推噓0(003)
留言3則, 2人參與, 最新討論串1/1
最近寫程式遇到一個怪問題,想請教一下 我有一個 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

09/29 17:13, , 1F
第一個提供正解的人我會送上 500p 幣作為答謝
09/29 17:13, 1F
※ 編輯: phantasma 來自: 72.130.134.2 (09/29 17:15)

09/29 17:45, , 2F
TcpThread.IsBackground = true;
09/29 17:45, 2F

09/29 18:14, , 3F
感謝樓上幫忙,p幣已經奉上~
09/29 18:14, 3F
文章代碼(AID): #1EX3PLs9 (C_Sharp)
文章代碼(AID): #1EX3PLs9 (C_Sharp)