等待集线器执行方法以继续 C#

Wait hub to execute on method to proceed C#

我正在进行集成测试并希望有一个方法等待“on method”被调用。在 C# 中。
像这样:

public async Task CheckMethod(){
    var hubConnection = GetHubConnection();
    connection.On("OnMethod",async () =>{
        Log("First");
    }
    await connection.InvokeAsync("Subscribe", id);
    // Something to wait then proceed
    Log("Second");
}

试试这个:

public async Task CheckMethod()
{
    var tcs = new TaskCompletionSource<object>();
    var hubConnection = GetHubConnection();
    connection.On("OnMethod",() =>
    {
        Log("First");
        tcs.TrySetResult(null); 
    }
    await connection.InvokeAsync("Subscribe", id);
    Log("Second");
    await tcs.Task;
    Log("Third");
}