NamedPipeClientStream - 无法取消连接

NamedPipeClientStream - no way to cancel Connect

我想使用 NamedPipeClientStream:

var _Pipe = new NamedPipeClientStream(".", "test-a", PipeDirection.In);
_Pipe.Connect();

很遗憾,无法传递取消令牌。那么,如何取消连接呢?我不想超时 - 我的客户端需要等待 "forever" 直到连接成功或请求取消。 ConnectAsync不可用。

编辑: 我正在使用 .NET Frameowkr 4.5.2 Class 库,并且没有像 ConnectAsync 这样的方法(只有 Connect 可用)。

要取消,请对该管道调用 Close() 或 Dispose() 方法。如果您稍后需要重试,只需为此创建另一个管道。

我有 90% 的把握在调用 Connect() 时休眠的线程会立即醒来,并因某些异常而失败。如果你调用 Dispose,可能是 ObjectDisposedException,如果你调用 Close(),可能是一些 "The pipe is being closed." Win32 异常。

我从来没有专门用管道测试过,但这通常是 Windows 阻塞 I/O 文件和套接字调用的情况。