AsyncCallback 未触发

AsyncCallback not firing

尝试在 WinForms 中实现异步 Client/Server 应用程序。客户端代码如下:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AsyncClient
{
    public partial class AsyncClient : Form
    {
        public AsyncClient()
        {
            InitializeComponent();
        }

        //ManualResetEvent for notifying asyncronous threads that an event has occured
        private static ManualResetEvent connectDone = new ManualResetEvent(false);
        private static ManualResetEvent sendDone = new ManualResetEvent(false);
        private static ManualResetEvent receiveDone = new ManualResetEvent(false);

        //Response from the remote device (server)
        private static String response = String.Empty;

        //start client
        private void button1_Click(object sender, EventArgs e)
        {
            TcpClient clientSocket = new TcpClient();
            try
            {
                var message = System.Text.Encoding.UTF8.GetBytes(txtFromClient.Text); //convert message to bytes for sending over the wire
                using (clientSocket)/*(var clientSocket = new TcpClient("127.0.0.1", 1234)) *///make connection with the host
                {
                    clientSocket.BeginConnect(IPAddress.Loopback, 1234, new AsyncCallback(connectCallBack), clientSocket);
                    sendDone.WaitOne();
                    lblConnectionStatus.Text = "Connect is successfully established with the remote device.";
                    //send data to remote device
                    NetworkStream stream = clientSocket.GetStream(); //obtain network stream
                    stream.Write(message, 0, message.Length); //send data to the remote device
                    //sendDone.WaitOne();
                }
            }
           finally
            {
                clientSocket.Close();
            }
        }

        private static void connectCallBack(IAsyncResult ar)
        {
            try
            {
                var clientSocket = (TcpClient)ar.AsyncState; //retrieve socket from state object (IAsyncResult)
                clientSocket.EndConnect(ar); //complete the connection
                connectDone.Set();
            }
            finally
            {

            }
        }

        private void txtFromClient_Click(object sender, EventArgs e)
        {
            txtFromClient.Text = "";
        }
    }
}

当我button1发短信时,UI卡住了。在 dubug 模式下,我发现 AsyncCallback(connectCallBack)

clientSocket.BeginConnect(IPAddress.Loopback, 1234, new AsyncCallback(connectCallBack), clientSocket); 

没有触发,因此 connectCallBack 没有被执行。程序在线暂停

sendDone.WaitOne();

知道为什么吗?有人可以帮忙吗?

您正在等待 sendDone.WaitOne();,您在回电中使用

connectDone.Set();

您应该将 sendDone.WaitOne(); 替换为 connectDone.WaitOne();

为什么 UI 冻结:因为您正尝试在主线程上进行连接,所以 UI 变得没有响应。这是因为 UI 在主线程上运行。要解决此问题,请尝试在与主要威胁不同的线程上进行连接或接收。

为什么不调用回调:你应该把sendDone.WaitOne();换成connectDone.WaitOne();因为你正在设置connectDone.Set()并且你正在等待sendDone.WaitOne();