3
我有一個程序,啓動一個服務器線程,該線程我等待用戶的連接,但有時(如果用戶按下「E」)我想關閉該服務器線程,但它不工作。我試圖殺多線程服務器的線程C#
'thread'.abort() - but it's freezing and not stopping the thread
這裏的服務器代碼:
private void start()
{
try
{
this.serverSocket = new TcpListener(8888); // creating the Server TCP socket
this.clientSocket = default(TcpClient); // creating the User socket variable
// starting the server
this.serverSocket.Start();
Console.WriteLine("->> server started"); // printing to log
// for always
while (true)
{
counter += 1; // new user
this.clientSocket = this.serverSocket.AcceptTcpClient(); // accepting user
Console.WriteLine(" ->> User connected"); // printing to log
// User creatinon
ConnectedUser user = new ConnectedUser(this.clientSocket);
user.startListerner();
}
this.clientSocket.Close();
this.serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
catch
{
Console.WriteLine("Error launching the Server");
}
}
,我跑這樣說:
this.server = new Thread(start);
server.Start();
我想終止它,我該怎麼辦呢?
http://stackoverflow.com/questions/2251964/c-sharp-thread-termination-and-thread-abort – xxbbcc