取消 NamedPipeClientStream.Read 调用
我查遍了整个网络,但找不到以下问题的答案。
我在客户端程序中有一个 C#/.NET NamedPipeClientStream 实例,并且工作线程正在调用 NamedPipeClientStream.Read(byte[], int, int) 从服务器获取数据。服务器向客户端发送数据更新。
Read 是一个阻塞调用。如果我想关闭客户端,有没有办法取消/退出 Read 调用?我尝试在命名管道实例上调用 Close,但它对调用 Read 的线程没有影响。
我认为有一种方法可以取消 Read 调用。如果不是,那么这似乎是一个设计非常糟糕的 API,因为您的程序受到管道的支配。
任何信息都非常感谢。
-克里斯
I've looked around all over the web, but I can't find an answer to the following question.
I have a C#/.NET NamedPipeClientStream instance in a client program, and a worker thread is calling NamedPipeClientStream.Read(byte[], int, int) to get data from a server. The server sends data updates to the client.
Read is a blocking call. If I want to close the client, is there a way to cancel/exit the Read call? I have tried calling Close on the named pipe instance, but it has no effect on the thread that called Read.
I would think there would be a way to cancel a Read call. If not, it seems like that is a very poorly designed API, because your program is at the mercy of the pipe.
Any info is greatly appreciated.
-Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用采用 PipeOptions 参数的 NamedPipeClientStream 构造函数。指定 PipeOptions.Asynchronous 将在调用 Close() 方法时完成 Read() 调用。 Read() 方法返回 0。
Use the NamedPipeClientStream constructor that takes a PipeOptions argument. Specifying PipeOptions.Asynchronous will complete the Read() call when you call the Close() method. The Read() method returns 0.
不是对你的问题的直接回答,但是 - 我最近不得不做一些 NamedPipe IPC,我发现 WCF 很棒。我花了不到一个小时的时间来实现 POC,是的,我很确定它也公开了取消您的请求的方法 - 而且您不必考虑字节数组。
这是你的选择吗?
Not a direct answer to your question, but - I had to do some NamedPipe IPC quite recently and I found that WCF was awesome. Took me less than an hour to implement a POC and yes, I'm pretty sure that it exposes means to cancel your request either - and you don't have to think in terms of byte arrays.
Is that's an option for you?