private async Task DisconnectCore(bool sendClose, CancellationToken token) { await clientConnectLock.WaitAsync(token).ConfigureAwait(false); try { List <Task> abortTasks = new List <Task>(); foreach (var channel in Channels) { abortTasks.Add(channel.Abort()); } await Task.WhenAll(abortTasks.ToArray()).ConfigureAwait(false); if (sendClose) { await ConnectionChannel.CloseConnection(token).ConfigureAwait(false); } await ChromecastSocketService.Disconnect(token).ConfigureAwait(false); } finally { clientConnectLock.Release(); } }
private async Task DisconnectCore(bool sendClose, bool disconnectSocket, CancellationToken token) { if (connected) { Connected = false; using (var sync = await clientConnectLock.LockAsync(token).ConfigureAwait(false)) { if (sendClose) { await ConnectionChannel.CloseConnection(token).ConfigureAwait(false); } if (disconnectSocket) { await ChromecastSocketService.Disconnect(token).ConfigureAwait(false); } var abortTasks = new List <Task>(); foreach (var channel in Channels) { abortTasks.Add(channel.Abort().WaitForFinishNoException()); } await Task.WhenAll(abortTasks.ToArray()).ConfigureAwait(false); } } }