From dd07fef5bbeffaec4d8fdc8a9de8cad36d1d0d33 Mon Sep 17 00:00:00 2001 From: Ionite Date: Mon, 25 Sep 2023 16:06:51 -0400 Subject: [PATCH] Fix ObjectDisposedException in Cancellation token access (#19) --- .../AdvancedImage.axaml.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs b/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs index a443e7d..4dc9997 100644 --- a/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs +++ b/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs @@ -189,8 +189,14 @@ private async void UpdateImage(string? source, IAsyncImageLoader? loader) var cancellationTokenSource = new CancellationTokenSource(); var oldCancellationToken = Interlocked.Exchange(ref _updateCancellationToken, cancellationTokenSource); - oldCancellationToken?.Cancel(); - oldCancellationToken?.Dispose(); + + try + { + oldCancellationToken?.Cancel(); + } + catch (ObjectDisposedException) + { + } if (source is null && CurrentImage is not ImageWrapper) { // User provided image himself @@ -232,6 +238,11 @@ private async void UpdateImage(string? source, IAsyncImageLoader? loader) { return null; } + finally + { + cancellationTokenSource.Dispose(); + } + }, CancellationToken.None); if (cancellationTokenSource.IsCancellationRequested) @@ -311,4 +322,4 @@ public void Draw(DrawingContext context, Rect sourceRect, Rect destRect) { /// public Size Size => ImageImplementation.Size; } -} \ No newline at end of file +}