Skip to content

Commit

Permalink
Fix a cancellation bug causing a partial image to sometimes be saved
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaily committed Apr 29, 2018
1 parent ec652da commit d1c6e49
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions MovieBarCodeGenerator/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -119,6 +119,7 @@ private async void generateButton_Click(object sender, EventArgs e)
});

_cancellationTokenSource = new CancellationTokenSource();
var cancellationLocalRef = _cancellationTokenSource;

// Actually create the barcode:

Expand All @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit d1c6e49

Please sign in to comment.