Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring for named pipes in C#

Tags:

c#

How can I monitor for named pipes? FileSystemWatcher cannot monitor \\\\.\pipes. Is there something similar to FSW that can be used for named pipes? 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.

like image 695
Murlex Avatar asked Nov 04 '22 14:11

Murlex


1 Answers

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.

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.

like image 76
Gabe Avatar answered Nov 15 '22 06:11

Gabe