From aa30c82985f3fb611329076d253e1556228ad4fc Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Mon, 1 Nov 2021 19:15:00 +0100 Subject: [PATCH] Ensure ConfigureAwait(false) is set (#246) Former-commit-id: e8df465ffa6e70277f0f29f093aef65cdd11a9f1 --- FFMpegCore/Extend/BitmapVideoFrameWrapper.cs | 2 +- FFMpegCore/Extend/PcmAudioSampleWrapper.cs | 2 +- FFMpegCore/FFMpeg/Arguments/PipeArgument.cs | 2 +- FFMpegCore/FFMpeg/FFMpeg.cs | 4 +-- FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs | 31 +++++++++++--------- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs b/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs index 678bdcb..2222db6 100644 --- a/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs +++ b/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs @@ -49,7 +49,7 @@ public async Task SerializeAsync(Stream stream, CancellationToken token) { var buffer = new byte[data.Stride * data.Height]; Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); - await stream.WriteAsync(buffer, 0, buffer.Length, token); + await stream.WriteAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false); } finally { diff --git a/FFMpegCore/Extend/PcmAudioSampleWrapper.cs b/FFMpegCore/Extend/PcmAudioSampleWrapper.cs index 503a23f..d6c1d2f 100644 --- a/FFMpegCore/Extend/PcmAudioSampleWrapper.cs +++ b/FFMpegCore/Extend/PcmAudioSampleWrapper.cs @@ -24,7 +24,7 @@ public void Serialize(Stream stream) public async Task SerializeAsync(Stream stream, CancellationToken token) { - await stream.WriteAsync(_sample, 0, _sample.Length, token); + await stream.WriteAsync(_sample, 0, _sample.Length, token).ConfigureAwait(false); } } } diff --git a/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs b/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs index fcb944a..c25df04 100644 --- a/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs @@ -40,7 +40,7 @@ public async Task During(CancellationToken cancellationToken = default) { try { - await ProcessDataAsync(cancellationToken); + await ProcessDataAsync(cancellationToken).ConfigureAwait(false); } catch (TaskCanceledException) { diff --git a/FFMpegCore/FFMpeg/FFMpeg.cs b/FFMpegCore/FFMpeg/FFMpeg.cs index 8ee59b8..6f0ede3 100644 --- a/FFMpegCore/FFMpeg/FFMpeg.cs +++ b/FFMpegCore/FFMpeg/FFMpeg.cs @@ -50,7 +50,7 @@ public static async Task SnapshotAsync(string input, string output, Size? if (Path.GetExtension(output) != FileExtension.Png) output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png; - var source = await FFProbe.AnalyseAsync(input); + var source = await FFProbe.AnalyseAsync(input).ConfigureAwait(false); var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex); return await arguments @@ -93,7 +93,7 @@ public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? capture /// Bitmap with the requested snapshot. public static async Task SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0) { - var source = await FFProbe.AnalyseAsync(input); + var source = await FFProbe.AnalyseAsync(input).ConfigureAwait(false); var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex); using var ms = new MemoryStream(); diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs index 0fac769..97ea94f 100644 --- a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs +++ b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs @@ -75,13 +75,7 @@ void OnCancelEvent(object sender, int timeout) try { - _ffMpegArguments.Pre(); - Task.WaitAll(instance.FinishedRunning().ContinueWith(t => - { - errorCode = t.Result; - cancellationTokenSource.Cancel(); - _ffMpegArguments.Post(); - }), _ffMpegArguments.During(cancellationTokenSource.Token)); + errorCode = Process(instance, cancellationTokenSource).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception e) { @@ -114,13 +108,7 @@ void OnCancelEvent(object sender, int timeout) try { - _ffMpegArguments.Pre(); - await Task.WhenAll(instance.FinishedRunning().ContinueWith(t => - { - errorCode = t.Result; - cancellationTokenSource.Cancel(); - _ffMpegArguments.Post(); - }), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false); + errorCode = await Process(instance, cancellationTokenSource).ConfigureAwait(false); } catch (Exception e) { @@ -134,6 +122,21 @@ await Task.WhenAll(instance.FinishedRunning().ContinueWith(t => return HandleCompletion(throwOnError, errorCode, instance.ErrorData); } + private async Task Process(Instance instance, CancellationTokenSource cancellationTokenSource) + { + var errorCode = -1; + + _ffMpegArguments.Pre(); + await Task.WhenAll(instance.FinishedRunning().ContinueWith(t => + { + errorCode = t.Result; + cancellationTokenSource.Cancel(); + _ffMpegArguments.Post(); + }), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false); + + return errorCode; + } + private bool HandleCompletion(bool throwOnError, int exitCode, IReadOnlyList errorData) { if (throwOnError && exitCode != 0)