C# 异步回调不触发
正在尝试在WinForms中实现异步客户端/服务器应用程序。客户端代码如下所示: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
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()代码>抱怨“不允许在未连接的套接字上执行此操作。”