Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Visual Basic
Article

Asynchronous Named Pipes (Overlapped IO) with VB.NET

Rate me:
Please Sign up or sign in to vote.
4.65/5 (10 votes)
6 Dec 20063 min read 99.5K   4.6K   41   24
An article on creating a library that takes advantage of the native Win32 overlapped IO for asynchronous named pipe communication.

Sample Image

Introduction

This article shows the fine points of taking advantage of the Windows Overlapped API for asynchronous communication through named pipes. The greatest achievement of this code compared to other similar ones is that the server and client actually stop when you tell them to.

Background

While I was trying to set up some means of inter-process communication between a Windows service and multiple instances of an application (one for each user), I stumbled upon various articles describing how to achieve this with named pipes. The idea seemed promising, but all the sample code I downloaded and tried out had the same weak point: the server loop (running on a different thread) locked while waiting for a client to connect. The only ways that one could use to make the server stop where:

  1. Thread.Abort

    I find this to be a very ugly solution since you can't really clean-up after the thread this way.

  2. Have a client connect to the server

    The service can connect to itself (!) in order for the server thread to unlock. This method is not very accurate, and shouts "workaround"! One of the articles I read seems to have the correct angle to solve the problem and is right here in the CodeProject, but.... oops! its in C++ :(.

Overlapped IO

All you have to do to use overlapped IO is to specify the FILE_FLAG_OVERLAPPED flag in the CreateNamedPipe (server side) and CreateFile (client side) APIs. From then on, everything is pretty easy since you can pass a handle to a ManualResetEvent to the OS (inside the NativeOverlapped structure) and it will signal you when your async operation is complete. This applies to all IO functions with an lpOverlapped parameter in their signature. Thus, ConnectNamedPipe (server side) becomes truly asynchronous, but also the ReadFile, WriteFile functions.

System.IO.FileStream

It seems strange that this class should be mentioned here, but if one the examined sample code on the named pipes, he would see that they are different from regular files only in how they are created (on the server side), while the handle that you obtain is in essence a file handle! So why not use the already existing FileStream class for IO operations? The constructor that receives a file handle came extremely handy here!

Using the code

I modeled (or at least tried to) the PipeListenner and PipeClient after the TCPListener and TCPClient classes, so their use is pretty straightforward:

PipeListenner

VB
Dim WithEvents Listenner As New _
    NamedPipes.PipeListener("myPipe")
Listenner.StartListenning

Private Sub Listenner_ClientConnected(ByVal sender As Object, _
        ByVal e As NamedPipes.ClientConnectedEventArgs) _
        Handles P.ClientConnected
    e.ClientStream.Read
    e.CleintStream.Write
    ...
End Sub

PipeClient

VB
Dim Client As New NamedPipes.PipeClient("\\.\pipe\myPipe")
Client.Connect
Client.ClientStream.Read
Client.ClientStream.Write
    ...

The preceding "code" demonstrates very simply how these classes are to be used. For a more advanced implementation (including multithreaded operation), download the demo application source code and take a look inside.

Points of interest

As explained by the before mentioned article by Rob Manders, all the "magic" is in this line of code:

VB
Select Case Threading.WaitHandle.WaitAny(mSyncEvents, -1, False)

What is peculiar about this piece of code is that it doesn't work... at least, not always! It seems to be working 99% while the application is executing under the "umbrella" of Visual Studio, but if you run the executable directly, it will crash miserably as soon as a client connects.

The alternative is:

VB
Select Case NativeMethods.WaitForMultipleObjects(mSyncEvents.Length, _
            mSyncEvents, False, UInteger.MaxValue)

but it will always fail if running under Visual Studio, but will work after many builds when running the release executable. I am really hoping that this is just caused by the fact that my box isn't at its best these days, because I can't really find an explanation for it! Maybe some of the people who will read this article may be able to offer some clue as to what might be going on here!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Greece Greece
Athens College Graduate - Class of '01
Deree College Student
IT Manager - Heliostat Ltd(www.heliostat.gr)

Comments and Discussions

 
Question5 stars and a tip to send data from client Pin
Predator7510-Aug-15 6:16
professionalPredator7510-Aug-15 6:16 
GeneralMy vote of 5 Pin
Global Analyser3-Nov-10 9:56
Global Analyser3-Nov-10 9:56 
Nice Code
QuestionC# code / article??? Pin
Robb Sadler2-Feb-09 9:44
Robb Sadler2-Feb-09 9:44 
QuestionDoes it work under a Windows service ? Pin
CleM7114-Jan-09 4:06
CleM7114-Jan-09 4:06 
GeneralNeet the Implementation using System.IO.Pipes Pin
mrjonie13-Nov-08 9:48
mrjonie13-Nov-08 9:48 
GeneralSystem.AccessViolationException Pin
Kotolom30-Jan-08 23:11
Kotolom30-Jan-08 23:11 
GeneralRe: System.AccessViolationException Pin
John Korres2-Feb-08 13:06
John Korres2-Feb-08 13:06 
GeneralRe: System.AccessViolationException Pin
Kotolom3-Feb-08 22:29
Kotolom3-Feb-08 22:29 
GeneralRe: System.AccessViolationException Pin
John Korres5-Feb-08 5:33
John Korres5-Feb-08 5:33 
GeneralWaitAny problem Pin
dlloyd29-Jul-07 4:29
dlloyd29-Jul-07 4:29 
GeneralRe: WaitAny problem Pin
BlueLou11-Nov-07 23:11
BlueLou11-Nov-07 23:11 
GeneralCrashing although 'final build' lines uncommented Pin
Ontario7016-Jul-07 3:33
Ontario7016-Jul-07 3:33 
GeneralRe: Crashing although 'final build' lines uncommented Pin
John Korres16-Jul-07 9:27
John Korres16-Jul-07 9:27 
QuestionWould anyone know where and how in this code I can set security so that the pipe accepts connections from anyone? Pin
daweinst22-Jun-07 4:42
daweinst22-Jun-07 4:42 
AnswerRe: Would anyone know where and how in this code I can set security so that the pipe accepts connections from anyone? Pin
John Korres4-Jul-07 20:00
John Korres4-Jul-07 20:00 
GeneralGreat Demo but...... Pin
SDI20036-Mar-07 5:31
SDI20036-Mar-07 5:31 
GeneralRe: Great Demo but...... Pin
SDI20036-Mar-07 6:05
SDI20036-Mar-07 6:05 
QuestionCrach On "Start Without Debugging". Pin
mruth0621-Dec-06 6:18
mruth0621-Dec-06 6:18 
AnswerRe: Crach On "Start Without Debugging". Pin
John Korres21-Dec-06 23:53
John Korres21-Dec-06 23:53 
GeneralRe: Crach On "Start Without Debugging". Pin
John Korres23-Dec-06 3:54
John Korres23-Dec-06 3:54 
GeneralGreat. Thanks a lot ! Pin
mruth0623-Dec-06 22:24
mruth0623-Dec-06 22:24 
GeneralRe: Crach On "Start Without Debugging". Pin
BigBenDk3-Aug-08 22:47
BigBenDk3-Aug-08 22:47 
GeneralThis is sweet. Pin
Marc Leger6-Dec-06 11:49
Marc Leger6-Dec-06 11:49 
GeneralRe: This is sweet. Pin
John Korres6-Dec-06 23:11
John Korres6-Dec-06 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.