取消 NamedPipeClientStream.Read 调用

发布于 2024-08-24 04:22:18 字数 354 浏览 6 评论 0原文

我查遍了整个网络,但找不到以下问题的答案。

我在客户端程序中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2024-08-31 04:22:18

使用采用 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.

_失温 2024-08-31 04:22:18

不是对你的问题的直接回答,但是 - 我最近不得不做一些 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文