简体   繁体   English

在C#中监视命名管道

[英]Monitoring for named pipes in C#

How can I monitor for named pipes? 如何监控命名管道? FileSystemWatcher cannot monitor \\\\\\\\.\\pipes . FileSystemWatcher无法监视\\\\\\\\.\\pipes Is there something similar to FSW that can be used for named pipes? 是否有类似于FSW的东西可以用于命名管道? Currently I poll for any new named pipe creation, but that seems ineffective a bit. 目前我轮询任何新的命名管道创建,但这似乎有点无效。

I want to be monitoring for pipes' creation that I am interested in. List of named pipes is good when the pipe is already created. 我想监视我感兴趣的管道创建。当管道已经创建时,命名管道列表很好。 I will have several instances of my program popping up and every time it pops up, I want to be able to connect with it using named pipes automatically without having to poll. 我将弹出我的程序的几个实例,每次弹出时,我希望能够使用命名管道自动连接它而无需轮询。 There seems to be no such mechanism available. 似乎没有这样的机制可用。

You can't watch for a named pipe per se , but you can wait for other things. 您无法查看命名管道本身 ,但您可以等待其他事情。 My solution to this is to create an event and then wait on that. 我的解决方案是创建一个事件,然后等待。 When the server program creates its pipe it also signals the event. 当服务器程序创建其管道时,它也会发出事件信号。 When the client gets signaled, close the event and open the pipe. 当客户端收到信号时,关闭事件并打开管道。

When you get an indication that the pipe has closed and you need to watch for it again, reopen the event and wait. 当您收到管道已关闭且需要再次注意的指示时,请重新打开该事件并等待。

If you have multiple pipes, you may want to have a separate event for each pipe ( EventForPipe-*pipename* ) or you may want to have one event that is used for all the pipes and simply check for each pipe when the event gets signalled. 如果您有多个管道,您可能希望为每个管道分别设置一个事件( EventForPipe-*pipename* ),或者您可能希望有一个事件用于所有管道,并在事件发出信号时检查每个管道。

This will work for a local pipe, but not a remote one because there's no way to signal or wait on a remote event. 这适用于本地管道,但不适用于远程管道,因为无法发出信号或等待远程事件。 If you have to watch for a pipe on another computer, the computer with the pipe can also have a shared directory that can be used for signalling. 如果您必须在另一台计算机上查看管道,则具有管道的计算机也可以具有可用于信令的共享目录。 Since a shared directory can be watched for file changes, you can create a file when the pipe is created. 由于可以监视共享目录以进行文件更改,因此可以在创建管道时创建文件。 Note that you still need to poll periodically (perhaps once per minute) for this strategy to work because a remote server may not be able to honor all remote file notification requests. 请注意,您仍需要定期轮询(可能每分钟一次)以使此策略生效,因为远程服务器可能无法遵守所有远程文件通知请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM