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 +}