Skip to content

Commit d1c6e49

Browse files
committed
Fix a cancellation bug causing a partial image to sometimes be saved
1 parent ec652da commit d1c6e49

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

MovieBarCodeGenerator/MainForm.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private async void generateButton_Click(object sender, EventArgs e)
105105
}
106106

107107
// Register progression callback and ready cancellation source:
108-
108+
109109
var progress = new PercentageProgressHandler(percentage =>
110110
{
111111
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)
119119
});
120120

121121
_cancellationTokenSource = new CancellationTokenSource();
122+
var cancellationLocalRef = _cancellationTokenSource;
122123

123124
// Actually create the barcode:
124125

@@ -140,7 +141,7 @@ private async void generateButton_Click(object sender, EventArgs e)
140141
await Task.Run(() =>
141142
{
142143
result = _imageProcessor.CreateBarCode(inputPath, parameters, _ffmpegWrapper, _cancellationTokenSource.Token, progress);
143-
});
144+
}, _cancellationTokenSource.Token);
144145
}
145146
catch (OperationCanceledException)
146147
{
@@ -167,6 +168,11 @@ Here is all the info available at the time of the error (press Ctrl+C to copy it
167168
_cancellationTokenSource = null;
168169
}
169170

171+
if (cancellationLocalRef.IsCancellationRequested)
172+
{
173+
return;
174+
}
175+
170176
// Save the barcode:
171177

172178
try
@@ -248,7 +254,8 @@ void ValidateOutputPath(ref string path)
248254
string smoothedOutputPath = null;
249255
if (smoothCheckBox.Checked)
250256
{
251-
smoothedOutputPath = $"{Path.GetFileNameWithoutExtension(outputPath)}_smoothed{Path.GetExtension(outputPath)}";
257+
var name = $"{Path.GetFileNameWithoutExtension(outputPath)}_smoothed{Path.GetExtension(outputPath)}";
258+
smoothedOutputPath = Path.Combine(Path.GetDirectoryName(outputPath), name);
252259
ValidateOutputPath(ref smoothedOutputPath);
253260
}
254261

0 commit comments

Comments
 (0)