Introducing Pipes [Justin Van Patten]

The Orcas October Community Technology Preview (CTP) includes new types that make it easy for developers to use pipes from managed code.  Pipes are used for inter-process communication (IPC) between processes running on the same machine, or processes running on any other Windows machine within a network.  We've added support for both anonymous and named pipes.

The new pipe types can be found in System.Core.dll within the System.IO namespace.  Note that after the October CTP went out, we moved the pipe types from System.IO to System.IO.Pipes, so the types will be in the new namespace in all future CTPs.

Anonymous Pipes

Anonymous pipes are character-based and are half-duplex.  They cannot communicate over the network and support only a single server instance.  These pipes are most useful for communication between threads or between parent and child processes where the pipe handles can be easily passed when the child process is created.

Example 1: Anonymous Pipes

The following example demonstrates sending a string from a parent process to a child process.  A line is read from the console of the parent and sent to the child process.  The child process then writes the string that it received from the parent process to the console.

Parent Process

Process process = new Process();

process.StartInfo.FileName = "child.exe";


using (AnonymousPipeServerStream pipeStream =

    new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) {


    process.StartInfo.Arguments = pipeStream.GetClientHandleAsString();

    process.StartInfo.UseShellExecute = false;





    using (StreamWriter sw = new StreamWriter(pipeStream)) {

        sw.AutoFlush = true;







Child Process

using (StreamReader sr = new StreamReader(

    new AnonymousPipeClientStream(PipeDirection.In, args[0]))) {

    string line;

    while ((line = sr.ReadLine()) != null) {

        Console.WriteLine("Echo: {0}", line);



Example output:

Anonymous pipes are sweet!

Echo: Anonymous pipes are sweet!

Named Pipes

Named pipes are far more powerful than anonymous pipes.  They can be duplex, over the network, and can support multiple server instances of a single name, making them ideal for quick-to-build, and easy to connect to, multithreaded servers.  Furthermore, they support message based communication so that a reading process can read varying-length messages precisely as sent by the writing process.  Finally, named pipes support impersonation allowing connecting processes to use their own set of permissions on remote servers.

Example 2: Named Pipes

In cases where the second process cannot inherit the pipe handle, named pipes can be used.  The following example demonstrates sending strings from the client process to the server process.

Server Process

using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("testpipe")) {



    using (StreamReader sr = new StreamReader(pipeStream)) {

        string temp;

        while ((temp = sr.ReadLine()) != null) {

            Console.WriteLine("{0}: {1}", DateTime.Now, temp);




Client Process

using (NamedPipeClientStream pipeStream = new NamedPipeClientStream("testpipe")) {



    using (StreamWriter sw = new StreamWriter(pipeStream)) {

        sw.AutoFlush = true;

        string temp;

        while ((temp = Console.ReadLine()) != null) {





Example 3: Named Pipes with Messaging

Named pipes also support message-based communication.  This allows a reading process to read varying-length messages precisely as sent by the writing process.  The following example displays how such messages are sent and read.

Process 1

UTF8Encoding encoding = new UTF8Encoding();


string message1 = "Named Pipe Message Example.";

string message2 = "Another Named Pipe Message Example.";

Byte[] bytes;


using (NamedPipeServerStream pipeStream = new

        NamedPipeServerStream("messagepipe", PipeDirection.InOut, 1,

        PipeTransmissionMode.Message, PipeOptions.None)) {



    // Let’s send two messages.

    bytes = encoding.GetBytes(message1);

    pipeStream.Write(bytes, 0, bytes.Length);


    bytes = encoding.GetBytes(message2);

    pipeStream.Write(bytes, 0, bytes.Length);


Process 2

Decoder decoder = Encoding.UTF8.GetDecoder();

Byte[] bytes = new Byte[10];

Char[] chars = new Char[10];


using (NamedPipeClientStream pipeStream =

        new NamedPipeClientStream("messagepipe")) {


    pipeStream.ReadMode = PipeTransmissionMode.Message;


    int numBytes;

    do {

        string message = "";

        do {

            numBytes = pipeStream.Read(bytes, 0, bytes.Length);

            int numChars = decoder.GetChars(bytes, 0, numBytes, chars, 0);

            message += new String(chars, 0, numChars);


        } while (!pipeStream.IsMessageComplete);






    } while (numBytes != 0);


The above three examples show how easy it is to achieve IPC using the new pipe types we're introducing in the Orcas release.  These types expose nearly all the pipe functionality provided by Windows.

In a future blog post we'll talk about some of the more advanced functionality that these types provide, such as multithreaded named pipes with impersonation and asynchronous IO.  In the meantime, we'd love to hear your feedback on the pipe functionality we're exposing.

Comments (14)
  1. You’ve been kicked (a good thing) – Trackback from

  2. B# .NET Blog says:

    Anonymous and named pipes are IPC mechanisms to communicate between processes. Basically, anonymous pipes

  3. Here they are: It looks like computers have finally got it over the humans with the defeat of Vladimir…

  4. CoqBlog says:

    Si vous étiez intéressé par le support des pipes, nommés ou anonymes, en .NET, vous avez probablement

  5. David Levine says:

    Is this a component that can be used with an existing 2.0 installation? If not, is there any chance that it could be rolled out separately so that it could be used with v2.0? Thanks

  6. Justin Van Patten says:

    The current plan is for this to be part of the Orcas release (i.e. .NET 3.5).  Right now there are no plans to roll this out as a separate component, but the idea is interesting.  Thanks for the feedback!

  7. Jeff Hua says:

    Great work!

    Will it be part of WCF(Windows Communication Foundation) solution? Although WCF is mainly intented for SOA developement, I am still wondering is it possible to develop high perofrmance application on that.

  8. Tim Hibbard on Changing the default report interval in TFS. Sean McBreen on Visual Studio Team System…

  9. Everyone used to talk about named pipes and pipes in general as the fastest way for inter-process communication

  10. Justin Van Patten says:


    This is not part of WCF at the moment.  Pipes are a lower level channel the WCF could be built on top of.

    WCF actually has its own internal pipe implementation that it uses to achieve IPC.  In the future, WCF may switch to using the public pipe implementation that we’re now providing.

  11. Mein TechTalk ist nun zu Ende. Meine letzte Station heute in Berlin war Lustig und Amüsant, ich hoffe

  12. I recently wrote an MSDN Flash article on the hidden gems of System.Core.dll of .NET Framework 3.5 which

  13. I recently wrote an MSDN Flash article on the hidden gems of System.Core.dll of .NET Framework 3.5 which

  14. .NET Framework 3.5 and Visual Studio 2008 have officially shipped! Soma has the announcement on his blog

Comments are closed.

Skip to main content