try/catch 中未发现命名管道错误

Error with Named Pipes is not caught in try/catch

我正在使用以下代码片段通过命名管道发送数据:

public void send(byte[] message, int timeOut = 1000)
{
    try
    {
        NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "TestPipe1234", PipeDirection.Out, PipeOptions.Asynchronous);

        pipeStream.Connect(timeOut);
        pipeStream.BeginWrite(message, 0, message.Length, aSyncSend, pipeStream);
    }
    catch (TimeoutException ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

有时,我会收到以下错误:

System.IO.IOException: The semaphore timeout period has expired.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.Pipes.NamedPipeClientStream.Connect(Int32 timeout)

[...]

我不确定这种情况何时发生(没有人在听另一方的声音?)但无论如何我不明白为什么这个错误没有被 try catch 块捕获。

你知道发生了什么事吗?欢迎任何帮助。

试试这个。

try
{
    NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "TestPipe1234", PipeDirection.Out, PipeOptions.Asynchronous);

    pipeStream.Connect(timeOut);
    pipeStream.BeginWrite(message, 0, message.Length, aSyncSend, pipeStream);
}
catch (Exception)
{
}