Bewerken

Delen via


NamedPipeClientStream.NumberOfServerInstances Property

Definition

Gets the number of server instances that share the same pipe name.

public:
 property int NumberOfServerInstances { int get(); };
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public int NumberOfServerInstances { get; }
public int NumberOfServerInstances { get; }
public int NumberOfServerInstances { [System.Security.SecurityCritical] get; }
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.NumberOfServerInstances : int
member this.NumberOfServerInstances : int
[<get: System.Security.SecurityCritical>]
member this.NumberOfServerInstances : int
Public ReadOnly Property NumberOfServerInstances As Integer

Property Value

The number of server instances that share the same pipe name.

Attributes

Exceptions

The pipe handle has not been set.

-or-

The current NamedPipeClientStream object has not yet connected to a NamedPipeServerStream object.

The pipe is broken or an I/O error occurred.

The underlying pipe handle is closed.

Examples

The following example demonstrates a method to send a string from a parent process to a child process using named pipes. In this example, a NamedPipeClientStream object is created in a child process, which then connects to a pipe on the local computer. The server example can be seen in the NamedPipeServerStream class. This example is part of a larger example provided for the NamedPipeServerStream and NamedPipeClientStream classes.

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

class PipeClient
{
    static void Main(string[] args)
    {
        using (NamedPipeClientStream pipeClient =
            new NamedPipeClientStream(".", "testpipe", PipeDirection.In))
        {

            // Connect to the pipe or wait until the pipe is available.
            Console.Write("Attempting to connect to pipe...");
            pipeClient.Connect();

            Console.WriteLine("Connected to pipe.");
            Console.WriteLine("There are currently {0} pipe server instances open.",
               pipeClient.NumberOfServerInstances);
            using (StreamReader sr = new StreamReader(pipeClient))
            {
                // Display the read text to the console
                string temp;
                while ((temp = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Received from server: {0}", temp);
                }
            }
        }
        Console.Write("Press Enter to continue...");
        Console.ReadLine();
    }
}
Imports System.IO
Imports System.IO.Pipes
Imports System.Security.Principal

Class PipeClient

    Shared Sub Main(ByVal args As String())

        Dim pipeClient As New NamedPipeClientStream("localhost", _
                    "testpipe", PipeDirection.In, PipeOptions.None)

        ' Connect to the pipe or wait until the pipe is available.
        Console.WriteLine("Attempting to connect to the pipe...")
        pipeClient.Connect()

        Console.WriteLine("Connect to the pipe.")
        Console.WriteLine("There are currently {0} pipe server instances open.", _
                          pipeClient.NumberOfServerInstances)

        Dim sr As New StreamReader(pipeClient)
        Dim temp As String

        temp = sr.ReadLine()
        While Not temp Is Nothing
            Console.WriteLine("Received from server: {0}", temp)
            temp = sr.ReadLine()
        End While
        Console.Write("Press Enter to continue...")
        Console.ReadLine()
    End Sub
End Class

Remarks

This property returns the number of server instances for the NamedPipeServerStream object that the current NamedPipeClientStream object has a handle to or is connected to. If the current NamedPipeClientStream object has not yet connected to a named pipe server, or if the current pipe handle has not yet been set, this property throws an InvalidOperationException.

Applies to