HttpListener.IsListening 属性

定义

获取一个值,指示 HttpListener 是否已启动。

public:
 property bool IsListening { bool get(); };
public bool IsListening { get; }
member this.IsListening : bool
Public ReadOnly Property IsListening As Boolean

属性值

如果已启动 HttpListener,则为 true;否则为 false

示例

下面的代码示例演示如何使用此属性来确定实例的侦听状态。

public static void DisplayPrefixesAndState(HttpListener listener)
{
    // List the prefixes to which the server listens.
    HttpListenerPrefixCollection prefixes = listener.Prefixes;
    if (prefixes.Count == 0)
    {
        Console.WriteLine("There are no prefixes.");
    }
    foreach(string prefix in prefixes)
    {
        Console.WriteLine(prefix);
    }
    // Show the listening state.
    if (listener.IsListening)
    {
        Console.WriteLine("The server is listening.");
    }
}
Public Shared Sub DisplayPrefixesAndState(ByVal listener As HttpListener)
    ' List the prefixes to which the server listens.
    Dim prefixes As HttpListenerPrefixCollection = listener.Prefixes

    If prefixes.Count = 0 Then
        Console.WriteLine("There are no prefixes.")
    End If

    For Each prefix As String In prefixes
        Console.WriteLine(prefix)
    Next

    ' Show the listening state.
    If listener.IsListening Then
        Console.WriteLine("The server is listening.")
    End If
End Sub

注解

若要启动 HttpListener,请 Start 调用 方法。

适用于