Click here to Skip to main content
14,027,427 members
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 6 Dec 2006

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


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.


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.


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:


Dim WithEvents Listenner As New _

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


Dim Client As New NamedPipes.PipeClient("\\.\pipe\myPipe")

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!


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


About the Author

John Korres
Greece Greece
Athens College Graduate - Class of '01
Deree College Student
IT Manager - Heliostat Ltd(

You may also be interested in...

Comments and Discussions

Question5 stars and a tip to send data from client Pin
Predator7510-Aug-15 6:16
memberPredator7510-Aug-15 6:16 
GeneralMy vote of 5 Pin
Global Analyser3-Nov-10 9:56
memberGlobal Analyser3-Nov-10 9:56 
QuestionC# code / article??? Pin
Robb Sadler2-Feb-09 9:44
memberRobb Sadler2-Feb-09 9:44 
QuestionDoes it work under a Windows service ? Pin
CleM7114-Jan-09 4:06
memberCleM7114-Jan-09 4:06 
GeneralNeet the Implementation using System.IO.Pipes Pin
mrjonie13-Nov-08 9:48
membermrjonie13-Nov-08 9:48 
GeneralSystem.AccessViolationException Pin
Kotolom30-Jan-08 23:11
memberKotolom30-Jan-08 23:11 
GeneralRe: System.AccessViolationException Pin
John Korres2-Feb-08 13:06
memberJohn Korres2-Feb-08 13:06 
GeneralRe: System.AccessViolationException Pin
Kotolom3-Feb-08 22:29
memberKotolom3-Feb-08 22:29 
GeneralRe: System.AccessViolationException Pin
John Korres5-Feb-08 5:33
memberJohn Korres5-Feb-08 5:33 
Just read the posts above and you will find out that I abandoned the version and have provided a C# version that works.


GeneralWaitAny problem Pin
dlloyd29-Jul-07 4:29
memberdlloyd29-Jul-07 4:29 
GeneralRe: WaitAny problem Pin
BlueLou11-Nov-07 23:11
memberBlueLou11-Nov-07 23:11 
GeneralCrashing although 'final build' lines uncommented Pin
Ontario7016-Jul-07 3:33
memberOntario7016-Jul-07 3:33 
GeneralRe: Crashing although 'final build' lines uncommented Pin
John Korres16-Jul-07 9:27
memberJohn 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
memberdaweinst22-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
memberJohn Korres4-Jul-07 20:00 
GeneralGreat Demo but...... Pin
SDI20036-Mar-07 5:31
memberSDI20036-Mar-07 5:31 
GeneralRe: Great Demo but...... Pin
SDI20036-Mar-07 6:05
memberSDI20036-Mar-07 6:05 
QuestionCrach On "Start Without Debugging". Pin
mruth0621-Dec-06 6:18
membermruth0621-Dec-06 6:18 
AnswerRe: Crach On "Start Without Debugging". Pin
John Korres21-Dec-06 23:53
memberJohn Korres21-Dec-06 23:53 
GeneralRe: Crach On "Start Without Debugging". Pin
John Korres23-Dec-06 3:54
memberJohn Korres23-Dec-06 3:54 
GeneralGreat. Thanks a lot ! Pin
mruth0623-Dec-06 22:24
membermruth0623-Dec-06 22:24 
GeneralRe: Crach On "Start Without Debugging". Pin
BigBenDk3-Aug-08 22:47
memberBigBenDk3-Aug-08 22:47 
GeneralThis is sweet. Pin
Marc Leger6-Dec-06 11:49
memberMarc Leger6-Dec-06 11:49 
GeneralRe: This is sweet. Pin
John Korres6-Dec-06 23:11
memberJohn 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190419.4 | Last Updated 6 Dec 2006
Article Copyright 2006 by John Korres
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid