[問題] 無法中斷的 thread
最近寫程式遇到一個怪問題,想請教一下
我有一個 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
09/29 17:13, 1F
※ 編輯: phantasma 來自: 72.130.134.2 (09/29 17:15)
→
09/29 17:45, , 2F
09/29 17:45, 2F
→
09/29 18:14, , 3F
09/29 18:14, 3F
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章