我遇到了SslStream.WriteAsync
的恼人问题这是代码
public void Send(PacketWriter writer)
{
var buffer = writer.GetWorkspace();
_sslStream.WriteAsync(buffer, 0, buffer.Length);
}
以极高的速度写入数据时,它告诉我
The beginwrite method cannot be called when another write operation is pending
[注意]:我的意思是像这样的高速
for (var i = 0 ; i <= 1000; i++)
{
Send(somedata);
}
答案 0 :(得分:1)
您必须等到上一次异步写入完成。
更改您的发送方式以返回Task
:
public Task Send(PacketWriter writer)
{
var buffer = writer.GetWorkspace();
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
}
调用方法async
,然后等待每个发送操作完成:
for (var i = 0 ; i <= 1000; i++)
{
await Send(somedata);
}
答案 1 :(得分:0)
可以尝试-
private static void send_data(SslStream socket_stream, byte[] data)
{
Task task = new Task(() => { socket_stream.Write(data, 0, data.Length); });
task.RunSynchronously();
task.Wait();
}
这将确保所有消息均以无错误的方式发送,