共用方式為


HubConnection.Closed 事件

定義

當連接關閉時發生。 連線可能會因為錯誤或伺服器或用戶端刻意關閉連線而關閉,而不會發生錯誤。

public:
 event Func<Exception ^, System::Threading::Tasks::Task ^> ^ Closed;
public event Func<Exception,System.Threading.Tasks.Task> Closed;
public event Func<Exception?,System.Threading.Tasks.Task>? Closed;
member this.Closed : Func<Exception, System.Threading.Tasks.Task> 
Public Event Closed As Func(Of Exception, Task) 

事件類型

範例

下列範例會將處理常式附加至 Closed 事件,並檢查提供的引數,以判斷是否有錯誤:

connection.Closed += (exception) =>
{
    if (exception == null)
    {
        Console.WriteLine("Connection closed without error.");
    }
    else
    {
        Console.WriteLine($"Connection closed due to an error: {exception}");
    }
};

備註

如果從連線錯誤觸發此事件,則發生的 會 Exception 當做這個處理常式的唯一引數傳入。 如果用戶端或伺服器刻意觸發此事件,則引數會是 null

適用於