private static async Task CleanupReader(SignalTask readTask, CancellationTokenSource cancellationTokenSource) { using (cancellationTokenSource) { using (readTask) { if (null != cancellationTokenSource) { try { if (!cancellationTokenSource.IsCancellationRequested) { cancellationTokenSource.Cancel(); } } catch (Exception ex) { Debug.WriteLine("HlsPlaylistSegmentManager.CleanupReader() cancel failed: " + ex.Message); } } if (null != readTask) { await readTask.WaitAsync().ConfigureAwait(false); } } } }
private async Task CloseAsync() { Debug.WriteLine("SmMediaManager.CloseAsync()"); TaskCompletionSource <object> closeTaskCompletionSource = new TaskCompletionSource <object>(); TaskCompletionSource <object> currentCloseTaskCompletionSource = Interlocked.CompareExchange(ref _closeTaskCompletionSource, closeTaskCompletionSource, null); if (null != currentCloseTaskCompletionSource) { object obj = await currentCloseTaskCompletionSource.Task.ConfigureAwait(false); Debug.WriteLine("SmMediaManager.CloseAsync() completed by other caller"); } else { State = MediaManagerState.Closing; TaskCompletionSource <object> playbackTaskCompletionSource = _playbackTaskCompletionSource; _closeCancellationTokenSource.Cancel(); ConfiguredTaskAwaitable configuredTaskAwaitable = CloseCleanupAsync().ConfigureAwait(false); await configuredTaskAwaitable; State = MediaManagerState.Closed; configuredTaskAwaitable = _reportStateTask.WaitAsync().ConfigureAwait(false); await configuredTaskAwaitable; Debug.WriteLine("SmMediaManager.CloseAsync() completed"); Interlocked.CompareExchange(ref _closeTaskCompletionSource, null, closeTaskCompletionSource); Task task = TaskEx.Run(() => { closeTaskCompletionSource.TrySetResult(null); playbackTaskCompletionSource.TrySetResult(null); }); TaskCollector.Default.Add(task, "SmMediaManager close"); } }