线程问题,为什么不能终止?(100分)

  • 主题发起人 主题发起人 蛮牛野蛮牛
  • 开始时间 开始时间

蛮牛野蛮牛

Unregistered / Unconfirmed
GUEST, unregistred user!
class WorkerThreadHandler {
public TcpListener myTcpListener;
public void HandleThread() {
Thread currentThread = Thread.CurrentThread;
Socket mySocket = myTcpListener.AcceptSocket();
string message =
"Thread Name: " + currentThread.Name +
"/r/nThread Apartment State: " + currentThread.ApartmentState.ToString() +
"/r/nThread State: " + currentThread.ThreadState.ToString();
Console.WriteLine(message);
byte[] buf = System.Text.Encoding.ASCII.GetBytes(message.ToCharArray());
mySocket.Send(buf);
Console.WriteLine("Closing connection with client.");
mySocket.Close();
}
}
public class MainThreadHandler {
private TcpListener myTcpListener;
public MainThreadHandler() {
myTcpListener = new TcpListener(10000);
myTcpListener.Start();
Console.WriteLine("Listener started. Press Ctrl+Break to stop.");
while (true) {
while (!myTcpListener.Pending()) {
Thread.Sleep(1000);
}
WorkerThreadHandler myWorkerThreadHandler = new WorkerThreadHandler();
myWorkerThreadHandler.myTcpListener = this.myTcpListener;
ThreadStart myThreadStart = new ThreadStart(myWorkerThreadHandler.HandleThread);
Thread myWorkerThread = new Thread(myThreadStart);
myWorkerThread.Name = "Created at " + DateTime.Now.ToString();
myWorkerThread.Start();
}
}
我用上面的代码的时候我关闭程序的时候线程不能结束,主要是ACCEPTSOCKET不能退出。
有什么好的方法???急。
 
看不懂哈![:D]
 
谢谢光顾!!!!为什么这样?
 
后退
顶部