Click here to Skip to main content
Click here to Skip to main content

Asynchronous Named Pipes (Overlapped IO) with VB.NET

, 6 Dec 2006
Rate this:
Please Sign up or sign in to vote.
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++ Frown | :(

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

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

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:

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:

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

Share

About the Author

John Korres

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

Comments and Discussions

 
GeneralMy vote of 5 PinmemberHeaven20203-Nov-10 10:56 
QuestionC# code / article??? PinmemberRobert Sadler2-Feb-09 10:44 
QuestionDoes it work under a Windows service ? PinmemberCleM7114-Jan-09 5:06 
GeneralNeet the Implementation using System.IO.Pipes Pinmembermrjonie13-Nov-08 10:48 
GeneralSystem.AccessViolationException PinmemberKotolom31-Jan-08 0:11 
GeneralRe: System.AccessViolationException PinmemberJohn Korres2-Feb-08 14:06 
GeneralRe: System.AccessViolationException PinmemberKotolom3-Feb-08 23:29 
GeneralRe: System.AccessViolationException PinmemberJohn Korres5-Feb-08 6:33 
GeneralWaitAny problem Pinmemberdlloyd29-Jul-07 5:29 
GeneralRe: WaitAny problem PinmemberBlueLou12-Nov-07 0:11 
GeneralCrashing although 'final build' lines uncommented PinmemberOntario7016-Jul-07 4:33 
GeneralRe: Crashing although 'final build' lines uncommented PinmemberJohn Korres16-Jul-07 10:27 
QuestionWould anyone know where and how in this code I can set security so that the pipe accepts connections from anyone? Pinmemberdaweinst22-Jun-07 5:42 
AnswerRe: Would anyone know where and how in this code I can set security so that the pipe accepts connections from anyone? PinmemberJohn Korres4-Jul-07 21:00 
GeneralGreat Demo but...... PinmemberSDI20036-Mar-07 6:31 
GeneralRe: Great Demo but...... PinmemberSDI20036-Mar-07 7:05 
QuestionCrach On "Start Without Debugging". Pinmembermruth0621-Dec-06 7:18 
AnswerRe: Crach On "Start Without Debugging". PinmemberJohn Korres22-Dec-06 0:53 
GeneralRe: Crach On "Start Without Debugging". PinmemberJohn Korres23-Dec-06 4:54 
GeneralGreat. Thanks a lot ! Pinmembermruth0623-Dec-06 23:24 
GeneralRe: Crach On "Start Without Debugging". PinmemberBigBenDk3-Aug-08 23:47 
GeneralThis is sweet. PinmemberMarc Leger6-Dec-06 12:49 
GeneralRe: This is sweet. PinmemberJohn Korres7-Dec-06 0:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 6 Dec 2006
Article Copyright 2006 by John Korres
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid