From d1c6e49905eb5c85d0a39a0328ebfad84a3c4dd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melvyn=20La=C3=AFly?= Date: Sun, 29 Apr 2018 12:36:51 +0200 Subject: [PATCH] Fix a cancellation bug causing a partial image to sometimes be saved --- MovieBarCodeGenerator/MainForm.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/MovieBarCodeGenerator/MainForm.cs b/MovieBarCodeGenerator/MainForm.cs index ebaf1c6..72aaecc 100644 --- a/MovieBarCodeGenerator/MainForm.cs +++ b/MovieBarCodeGenerator/MainForm.cs @@ -105,7 +105,7 @@ private async void generateButton_Click(object sender, EventArgs e) } // Register progression callback and ready cancellation source: - + var progress = new PercentageProgressHandler(percentage => { var progressBarValue = Math.Min(100, (int)Math.Round(percentage * 100, MidpointRounding.AwayFromZero)); @@ -119,6 +119,7 @@ private async void generateButton_Click(object sender, EventArgs e) }); _cancellationTokenSource = new CancellationTokenSource(); + var cancellationLocalRef = _cancellationTokenSource; // Actually create the barcode: @@ -140,7 +141,7 @@ private async void generateButton_Click(object sender, EventArgs e) await Task.Run(() => { result = _imageProcessor.CreateBarCode(inputPath, parameters, _ffmpegWrapper, _cancellationTokenSource.Token, progress); - }); + }, _cancellationTokenSource.Token); } catch (OperationCanceledException) { @@ -167,6 +168,11 @@ Here is all the info available at the time of the error (press Ctrl+C to copy it _cancellationTokenSource = null; } + if (cancellationLocalRef.IsCancellationRequested) + { + return; + } + // Save the barcode: try @@ -248,7 +254,8 @@ void ValidateOutputPath(ref string path) string smoothedOutputPath = null; if (smoothCheckBox.Checked) { - smoothedOutputPath = $"{Path.GetFileNameWithoutExtension(outputPath)}_smoothed{Path.GetExtension(outputPath)}"; + var name = $"{Path.GetFileNameWithoutExtension(outputPath)}_smoothed{Path.GetExtension(outputPath)}"; + smoothedOutputPath = Path.Combine(Path.GetDirectoryName(outputPath), name); ValidateOutputPath(ref smoothedOutputPath); }