Like apparently 1 million other developers (according to Google) I needed to set up a named pipe in .Net. The problem is that what I found was either overkill or buggy. So I started with the code at Paranoid Ferret, fixed the bugs I found (you need to open the streams before returning from the connect method), and added a Dispose() to both sides.
This code (NamedPipes.zip) is designed to be a very simple system where a server and multiple clients can send text messages to each other asynchronously. And note that the messages come in on the pipe's threads so you need to Invoke to the UI thread if you are going to touch any of the UI.
This code could use improvement.
- I tried running it in message mode but the call on the client side to put it in message mode failed. If anyone can tell me why or can fix it, please let me know.
-
The blocking reads on both sides should use events so that the thread can be killed by triggering the event. And the OVERLAPPED struct should be created with an event on the server side for this same purpose. I did not do this because I don't know the SDK events real well and I saw conflicting recommendations about how to use them here. Again, if you know the correct way to do this, please let me know.
This code has been thoroughly tested here and is 100% solid, at least in how we are using it.

Comments