我有一个namedpipeServerStream,当我遇到超时时,我想停止阅读它的管道。
这是我的Read和Read回调函数,我在主程序循环中反复调用Read,实际上我的问题是,当endread超时时,我如何手动(或以任何其他方式)调用回调函数。
private void Read()
{
tmpBuff = new byte[600];
inRead = true;
ReadTimer.Enabled = true;
len = 0;
try
{
_namedpipeserver.BeginRead(tmpBuff, 0, 600, ReadCallback, null);
}
catch (Exception ex) //disconnected/disposed
{
return;
}
}
static void ReadCallback(IAsyncResult ar)
{
int readbyte;
try
{
readbyte = _namedpipeserver.EndRead(ar);
if (readbyte > 0)
{
len = readbyte;
int packetLen = tmpBuff[0] + (tmpBuff[1] * 256);
Console.WriteLine(" packet len: " + packetLen + " bytes ");
readbyte = 0;
array = null;
array = new byte[packetLen];
Array.Copy(tmpBuff, 2, array, 0, packetLen);
}
}
catch (IOException) //closed
{
return;
}
inRead = false;
}
任何帮助Appreciated.Thanks
发布于 2022-09-24 07:00:26
我通过在定义的超时后配置连接并等待管道的另一端再次连接来解决此问题。
Task.Run(() =>
{
tmpBuff = new byte[600];
readbyte = _namedpipeserver.Read(tmpBuff, 0, 600);
}).Wait(10000);
if (readbyte <= 0)
return null;
如果返回null,请尝试重新启动管道。
https://stackoverflow.com/questions/67470768
复制相似问题