A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Fix CheckSum computation cancellation (#281)
João Felício5df0cb834acb
diff --git a/src/dev/impl/DevToys/ViewModels/Tools/Generators/CheckSumGenerator/CheckSumGeneratorToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/Generators/CheckSumGenerator/CheckSumGeneratorToolViewModel.csindex c43b4e4a1..c8112ce6f 100644--- a/src/dev/impl/DevToys/ViewModels/Tools/Generators/CheckSumGenerator/CheckSumGeneratorToolViewModel.cs+++ b/src/dev/impl/DevToys/ViewModels/Tools/Generators/CheckSumGenerator/CheckSumGeneratorToolViewModel.cs@@ -66,6 +66,7 @@ namespace DevToys.ViewModels.Tools.CheckSumGeneratorprivate bool _hasCancelledCalculation;private bool _shouldDisplayProgress;private HashingAlgorithmDisplayPair? _outputHashingAlgorithm;+ private readonly Queue<Task> _computationTaskQueue = new();internal CheckSumGeneratorStrings Strings => LanguageManager.Instance.CheckSumGenerator;@@ -162,7 +163,6 @@ namespace DevToys.ViewModels.Tools.CheckSumGeneratorHashingAlgorithmDisplayPair.SHA512,};-[ImportingConstructor]public CheckSumGeneratorToolViewModel(ISettingsProvider settingsProvider, IMarketingService marketingService){@@ -282,9 +282,9 @@ namespace DevToys.ViewModels.Tools.CheckSumGeneratorlock (_lockObject){_hasCancelledCalculation = true;+_cancellationTokenSource.Cancel();_cancellationTokenSource.Dispose();-_cancellationTokenSource = new CancellationTokenSource();if (shouldRevertHashingAlgo && _outputHashingAlgorithm != null)@@ -295,6 +295,7 @@ namespace DevToys.ViewModels.Tools.CheckSumGenerator_isCalculationInProgress = false;_hasCancelledCalculation = false;ShouldDisplayProgress = false;+ResetProgress();}}@@ -323,7 +324,13 @@ namespace DevToys.ViewModels.Tools.CheckSumGeneratorlock (_lockObject){ExecuteCancelCommand(shouldRevertHashingAlgo: false);- CheckSumAsync().Forget();++ if (_computationTaskQueue.Count > 0 && _computationTaskQueue.Peek().IsCompleted)+ {+ _computationTaskQueue.Dequeue();+ }++ _computationTaskQueue.Enqueue(CheckSumAsync());}}@@ -338,6 +345,11 @@ namespace DevToys.ViewModels.Tools.CheckSumGeneratorawait TaskScheduler.Default;+ if (_computationTaskQueue.Count > 1)+ {+ await _computationTaskQueue.Dequeue();+ }+using Stream? fileStream = await InputFile.OpenStreamForReadAsync();await ThreadHelper.RunOnUIThreadAsync(() => ShouldDisplayProgress = HashingHelper.ComputeHashIterations(fileStream) > _displayAboveIterations);@@ -346,15 +358,15 @@ namespace DevToys.ViewModels.Tools.CheckSumGeneratorawait ThreadHelper.RunOnUIThreadAsync(() =>{+ _isCalculationInProgress = false;+ ShouldDisplayProgress = false;+if (hashOutput != null){Output = hashOutput;_outputHashingAlgorithm = InputHashingAlgorithm;}- _isCalculationInProgress = false;- ShouldDisplayProgress = false;-if (!_toolSuccessfullyWorked){_toolSuccessfullyWorked = true;@@ -427,6 +439,5 @@ namespace DevToys.ViewModels.Tools.CheckSumGeneratorHashingAlgorithm.SHA512 => SHA512.Create(),_ => throw new ArgumentException("Hash Algorithm not supported", nameof(HashingAlgorithm))};-}}