C# 停止线程从另一个线程c侦听TcpClient#

C# 停止线程从另一个线程c侦听TcpClient#,c#,multithreading,C#,Multithreading,我目前正在制作c#格式的客户端/服务器应用程序。该窗口有日志(文本区域)、命令行(文本区域)和一个按钮。在服务器运行时关闭服务器(另一个线程)时出现问题 void Server_Thread_Main(object arg) { TcpClient client; Output.Insert_to_LOG_Window("Server initialized at IP: " + IP_ADDR + ":" + PORT); whi

我目前正在制作c#格式的客户端/服务器应用程序。该窗口有日志(文本区域)、命令行(文本区域)和一个按钮。在服务器运行时关闭服务器(另一个线程)时出现问题

    void Server_Thread_Main(object arg)
    {
        TcpClient client;
        Output.Insert_to_LOG_Window("Server initialized at IP: " + IP_ADDR + ":" + PORT);
        while (true)
        {
            client = listener.AcceptTcpClient();
            clients.Add(new C_Client(client, Output, client_ID_iter++));

            clients[clients.Count - 1].Start();
        }




    }
我还有一个类,用于处理输入到命令行的命令。它叫任务动物园。每个命令都由新线程处理,以便处理它具有此功能的命令

    void Server_Thread_Main(object arg)
    {
        TcpClient client;
        Output.Insert_to_LOG_Window("Server initialized at IP: " + IP_ADDR + ":" + PORT);
        while (true)
        {
            client = listener.AcceptTcpClient();
            clients.Add(new C_Client(client, Output, client_ID_iter++));

            clients[clients.Count - 1].Start();
        }




    }
    void Server_Task_CMD(optargs args)
    {
        bool if_start = false;

        foreach(argument arg in args)
        {
            switch (arg.Option)
            {
                case "-stop":
                    Output.server.Stop_Server(); //<- stopping server
                    break;
                case "-start":
                    if_start = true;
                    break;
                case "-IP":
                    Output.server.Set_Server_IP(arg.Value);
                    break;
                case "-PORT":
                    Output.server.Set_Server_PORT(arg.Value);
                    break;
                case "-IPP":
                    if (arg.Value.Contains(':') == true)
                        Output.server.Set_Server_Params(arg.Value);
                    else
                        Output.Insert_to_COMMAND_Log("This " + arg.Value + " isn't valid IP:PORT notation!");
                    break;
                case "-V":
                case "-VIEW":
                    Output.server.Display_IP_PORT();
                    break;
                default:
                    Output.Insert_to_COMMAND_Log("Not known command: " + arg.Option + " !");
                    break;



            }
            if(if_start==true)
                Output.server.Initialize_Server();

        }
    }

问题是我不知道为什么服务器线程没有停止

这不是靠近螺纹的正确方法。当前,您的线程可能卡在
侦听器上。AcceptTcpClient

    void Server_Thread_Main(object arg)
    {
        TcpClient client;
        Output.Insert_to_LOG_Window("Server initialized at IP: " + IP_ADDR + ":" + PORT);
        while (true)
        {
            client = listener.AcceptTcpClient();
            clients.Add(new C_Client(client, Output, client_ID_iter++));

            clients[clients.Count - 1].Start();
        }




    }
您应该首先使用
listener.Stop()关闭侦听器
其次,您必须关闭所有客户端套接字。
第三,除非绝对没有其他方法,否则不应该调用
thread.Abort

    void Server_Thread_Main(object arg)
    {
        TcpClient client;
        Output.Insert_to_LOG_Window("Server initialized at IP: " + IP_ADDR + ":" + PORT);
        while (true)
        {
            client = listener.AcceptTcpClient();
            clients.Add(new C_Client(client, Output, client_ID_iter++));

            clients[clients.Count - 1].Start();
        }




    }
因此,您的
Stop\u服务器
应该更像这样:

    void Server_Thread_Main(object arg)
    {
        TcpClient client;
        Output.Insert_to_LOG_Window("Server initialized at IP: " + IP_ADDR + ":" + PORT);
        while (true)
        {
            client = listener.AcceptTcpClient();
            clients.Add(new C_Client(client, Output, client_ID_iter++));

            clients[clients.Count - 1].Start();
        }




    }
public void Stop_Server()
{
    listener.Stop();
    server_thread.Join();
    Output.Insert_to_COMMAND_Log("Server has been stopped!!!");
}
在您的线程方法中,将
while(true)
try catch(SocketException)

    void Server_Thread_Main(object arg)
    {
        TcpClient client;
        Output.Insert_to_LOG_Window("Server initialized at IP: " + IP_ADDR + ":" + PORT);
        while (true)
        {
            client = listener.AcceptTcpClient();
            clients.Add(new C_Client(client, Output, client_ID_iter++));

            clients[clients.Count - 1].Start();
        }




    }
了解更多信息

谢谢,这很有帮助:)我知道它在“client=listener.AcceptTcpClient();”上停止,但我不知道我可以从另一个线程阻止它。
    void Server_Thread_Main(object arg)
    {
        TcpClient client;
        Output.Insert_to_LOG_Window("Server initialized at IP: " + IP_ADDR + ":" + PORT);
        while (true)
        {
            client = listener.AcceptTcpClient();
            clients.Add(new C_Client(client, Output, client_ID_iter++));

            clients[clients.Count - 1].Start();
        }




    }