Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ReadMode Property
Collapse the table of content
Expand the table of content

AnonymousPipeServerStream.ReadMode Property

Sets the reading mode for the AnonymousPipeServerStream object. For anonymous pipes, transmission mode must be Byte.

Namespace:  System.IO.Pipes
Assembly:  System.Core (in System.Core.dll)

public override PipeTransmissionMode ReadMode { set; }

Property Value

Type: System.IO.Pipes.PipeTransmissionMode
The reading mode for the AnonymousPipeServerStream object.

ExceptionCondition
ArgumentOutOfRangeException

The transmission mode is not valid. For anonymous pipes, only Byte is supported.

NotSupportedException

The property is set to Message, which is not supported for anonymous pipes.

IOException

The connection is broken or another I/O error occurs.

ObjectDisposedException

The pipe is closed.

Anonymous pipes do not support Message read mode.

The following example demonstrates a way to send a string from a parent process to a child process by using anonymous pipes. In this example, the NotSupportedException is caught when the ReadMode property is set to Message.

using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeServer
{
    static void Main()
    {
        Process pipeClient = new Process();

        pipeClient.StartInfo.FileName = "pipeClient.exe";

        using (AnonymousPipeServerStream pipeServer =
            new AnonymousPipeServerStream(PipeDirection.Out,
            HandleInheritability.Inheritable))
        {
            // Show that anonymous pipes do not support Message mode. 
            try
            {
                Console.WriteLine("[SERVER] Setting ReadMode to \"Message\".");
                pipeServer.ReadMode = PipeTransmissionMode.Message;
            }
            catch (NotSupportedException e)
            {
                Console.WriteLine("[SERVER] Exception:\n    {0}", e.Message);
            }

            Console.WriteLine("[SERVER] Current TransmissionMode: {0}.",
                pipeServer.TransmissionMode);

            // Pass the client process a handle to the server.
            pipeClient.StartInfo.Arguments =
                pipeServer.GetClientHandleAsString();
            pipeClient.StartInfo.UseShellExecute = false;
            pipeClient.Start();

            pipeServer.DisposeLocalCopyOfClientHandle();

            try
            {
                // Read user input and send that to the client process. 
                using (StreamWriter sw = new StreamWriter(pipeServer))
                {
                    sw.AutoFlush = true;
                    // Send a 'sync message' and wait for client to receive it.
                    sw.WriteLine("SYNC");
                    pipeServer.WaitForPipeDrain();
                    // Send the console input to the client process.
                    Console.Write("[SERVER] Enter text: ");
                    sw.WriteLine(Console.ReadLine());
                }
            }
            // Catch the IOException that is raised if the pipe is broken 
            // or disconnected. 
            catch (IOException e)
            {
                Console.WriteLine("[SERVER] Error: {0}", e.Message);
            }
        }

        pipeClient.WaitForExit();
        pipeClient.Close();
        Console.WriteLine("[SERVER] Client quit. Server terminating.");
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft