.net 如何在异步方法中使用AcceptCpcClient

.net 如何在异步方法中使用AcceptCpcClient,.net,visual-studio-2010,tcp,c++-cli,.net,Visual Studio 2010,Tcp,C++ Cli,我正在用C++/CLI编写一个使用TcpListener的小程序,我知道AcceptTcpClient是一种同步方法,它会阻塞UI线程。如何调用此方法,使其不会阻塞我的UI?这是我的密码: TcpClient^ client = server->AcceptTcpClient(); sendConsoleResult( "Connected to Client, waiting for for instructions..." ); data = nullptr; // Get a str

我正在用C++/CLI编写一个使用
TcpListener
的小程序,我知道
AcceptTcpClient
是一种同步方法,它会阻塞UI线程。如何调用此方法,使其不会阻塞我的UI?这是我的密码:

TcpClient^ client = server->AcceptTcpClient();
sendConsoleResult( "Connected to Client, waiting for for instructions..." );
data = nullptr;
// Get a stream Object* for reading and writing
NetworkStream^ stream = client->GetStream();
Int32 i;
// Loop to receive all the data sent by the client.
while ( i = stream->Read( bytes, 0, bytes->Length ))
{
    // Translate data bytes to a ASCII String*.
    data = Encoding::ASCII->GetString( bytes, 0, i );
    sendConsoleResult("[RECEIVED]: "+ data);
    // Process the data then send to command processing module
    data = data->ToUpper();
    response = commandProcess(data)->ToUpper();
    array<Byte>^msg = Encoding::ASCII->GetBytes( response );
    // Send back a response.
    stream->Write( msg, 0, msg->Length );
    sendConsoleResult("[RESPONSE]: " + response);
}
TcpClient^client=server->AcceptTcpClient();
sendConsoleResult(“已连接到客户端,正在等待指示…”);
数据=空PTR;
//获取用于读写的流对象*
NetworkStream ^stream=client->GetStream();
int32i;
//循环以接收客户端发送的所有数据。
而(i=流->读取(字节、0、字节->长度))
{
//将数据字节转换为ASCII字符串*。
数据=编码::ASCII->GetString(字节,0,i);
sendConsoleResult(“[接收]:”+数据);
//处理数据,然后发送到命令处理模块
data=data->ToUpper();
response=commandProcess(data)->ToUpper();
数组^msg=编码::ASCII->GetBytes(响应);
//发回回复。
流->写入(消息,0,消息->长度);
sendConsoleResult(“[响应]:”+响应);
}
感谢您回答我的问题:

切换到使用,以及使用。这将使整个操作异步,而不会阻塞用户界面线程