Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步回调不触发_C#_Asp.net_Winforms_Asyncsocket - Fatal编程技术网

C# 异步回调不触发

C# 异步回调不触发,c#,asp.net,winforms,asyncsocket,C#,Asp.net,Winforms,Asyncsocket,正在尝试在WinForms中实现异步客户端/服务器应用程序。客户端代码如下所示: 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

正在尝试在WinForms中实现异步客户端/服务器应用程序。客户端代码如下所示:

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 = "";
        }
    }
}
当我按下
按钮1
发送文本时,用户界面冻结。在dubug模式下,我发现
AsyncCallback(connectCallBack)
在同一行

clientSocket.BeginConnect(IPAddress.Loopback, 1234, new AsyncCallback(connectCallBack), clientSocket); 
未触发,因此未执行
connectCallBack
。该程序相当于在线暂停

sendDone.WaitOne();

你知道为什么吗?有人能帮忙吗

您正在等待
sendDone.WaitOne()并在回拨中使用

connectDone.Set();

您应该替换
sendDone.WaitOne()
connectDone.WaitOne()一起使用

您正在等待
sendDone.WaitOne()并在回拨中使用

connectDone.Set();

您应该替换
sendDone.WaitOne()
connectDone.WaitOne()一起使用

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


为什么不调用回调:应该替换
sendDone.WaitOne()
connectDone.WaitOne()一起使用connectDone.Set()
并且正在等待
sendDone.WaitOne()

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


为什么不调用回调:应该替换
sendDone.WaitOne()
connectDone.WaitOne()一起使用connectDone.Set()
并且正在等待
sendDone.WaitOne()

检查检查
connectDone.Set()解决了回调问题。用户界面也不再冻结。在远程设备上成功接收到来自客户端的第一次发送。问题是在客户端发送第二次发送。在线引发
NetworkStream=clientSocket.GetStream()抱怨
不允许在未连接的套接字上执行此操作。
。这是因为您的套接字已关闭,或者在
最后{clientSocket.Close();}
connectDone.Set()解决了回调问题。用户界面也不再冻结。在远程设备上成功接收到来自客户端的第一次发送。问题是在客户端发送第二次发送。在线引发
NetworkStream=clientSocket.GetStream()抱怨
不允许在未连接的套接字上执行此操作。
。这是因为您的套接字已关闭,或者在
最后{clientSocket.Close();}
connectDone.Set()解决了回调问题。用户界面也不再冻结。在远程设备上成功接收到来自客户端的第一次发送。问题是在客户端发送第二次发送。在线引发
NetworkStream=clientSocket.GetStream()抱怨“不允许在未连接的套接字上执行此操作”。
connectDone.Set()解决了回调问题。用户界面也不再冻结。在远程设备上成功接收到来自客户端的第一次发送。问题是在客户端发送第二次发送。在线引发
NetworkStream=clientSocket.GetStream()抱怨“不允许在未连接的套接字上执行此操作。”