Share via


NamedPipeClientStream.NumberOfServerInstances 屬性

定義

取得共用相同管道名稱的伺服器執行個體數目。

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

屬性值

共用相同管道名稱的伺服器執行個體數目。

屬性

例外狀況

尚未設定管道控制代碼。

-或-

目前的 NamedPipeClientStream 物件尚未連接到 NamedPipeServerStream 物件。

管道中斷或發生 I/O 錯誤。

基礎的管道控制代碼已關閉。

範例

下列範例示範使用命名管道將字串從父進程傳送至子進程的方法。 在此範例中, NamedPipeClientStream 物件會在子進程中建立,然後連接到本機計算機上的管道。 您可以在 類別中看到 NamedPipeServerStream 伺服器範例。 此範例是 針對和 NamedPipeClientStream 類別提供的較大範例的NamedPipeServerStream一部分。

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

備註

這個屬性會傳回目前NamedPipeClientStream物件具有句柄或所連接之對象的伺服器實例NamedPipeServerStream數目。 如果目前的 NamedPipeClientStream 物件尚未連線到具名管道伺服器,或目前管道句柄尚未設定,則這個屬性會 InvalidOperationException擲回 。

適用於