将数据从后台线程发送到主线程

本文关键字:线程 后台 数据 | 更新日期: 2023-09-27 18:12:39

我正在从主应用程序运行c#服务器,我想将从服务器线程收到的消息传递给主线程。对于新的连接,服务器应该在后台运行。每次有一个新的连接,服务器应该将收到的消息传递给主应用程序。如何让主应用程序知道何时收到消息?当有一个新连接时,如何将消息从服务器线程传递到主线程?

主应用程序

  public partial class MainWindow : Window
 {
        TCPServer Server = new TCPServer(); //start running the server
        //get the message (Server.message) when a client sent it to the server
        //TODO process the message
    }
TCP服务器

class TCPServer
    {
        private TcpListener tcpListener;
        private Thread listenThread;
        private String message;
        public TCPServer()
    {
        this.tcpListener = new TcpListener(IPAddress.Any, 3200);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }
 //starts the tcp listener and accept connections
    private void ListenForClients()
    {
        this.tcpListener.Start();
        while (true)
        {
            //blocks until a client has connected to the server
            System.Diagnostics.Debug.WriteLine("Listening...");
            TcpClient client = this.tcpListener.AcceptTcpClient();
            System.Diagnostics.Debug.WriteLine("Client connected");

            //create a thread to handle communication 
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }

    //Read the data from the client
    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client; //start the client
        NetworkStream clientStream = tcpClient.GetStream(); //get the stream of data for network access
        byte[] message = new byte[4096];
        int bytesRead;
        while (true) 
        {
            bytesRead = 0;
            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                break;
            }
            if (bytesRead == 0) //if we receive 0 bytes
            {
                //the client has disconnected from the server 

          break;
                }
                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                message = encoder.GetString(message, 0, bytesRead);
                //Reply
                byte[] buffer = encoder.GetBytes("ACK");
                clientStream.Write(buffer, 0, buffer.Length);
                System.Diagnostics.Debug.WriteLine("ACK");
                clientStream.Flush();
               }
            tcpClient.Close();
            System.Diagnostics.Debug.WriteLine("Client disconnected");
        }

将数据从后台线程发送到主线程

TcpListener已经很好地支持了,使用BeginAcceptTcpClient()方法代替。当你从WPF或Winforms应用程序的主线程调用它时,回调将自动运行在同一个主线程上。它的BeginReceive()方法也是如此。在内部,它使用dispatcher循环来激活回调方法,非常类似于BackgroundWorker类和c# v5 async/await关键字的工作方式。

这使您避免了启动和终止您自己的线程的麻烦,并确保您正确地封送回。并且大大减少了程序的资源使用。强烈推荐。

队列是答案。具体来说,在这种情况下是并发队列。

套接字线程将消息放入队列。您的工作线程轮询队列并提取工作项。

对于基于套接字的应用程序,此模式非常非常常见。

或者,您可以针对系统线程池QueueUserWorkItem,并让它管理工作负载。

注意:您现在处于多线程领域。您需要了解同步和其他可能出现的问题。如果做不到这一点,你的应用就会出现奇怪的bug。