Skip to content

Commit

Permalink
Merge pull request #20 from ionite34/patch-1
Browse files Browse the repository at this point in the history
Fix ObjectDisposedException in Cancellation token access (#19)
  • Loading branch information
SKProCH authored Sep 25, 2023
2 parents 1102e6e + dd07fef commit 7c543ab
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -232,6 +238,11 @@ private async void UpdateImage(string? source, IAsyncImageLoader? loader)
{
return null;
}
finally
{
cancellationTokenSource.Dispose();
}

}, CancellationToken.None);

if (cancellationTokenSource.IsCancellationRequested)
Expand Down Expand Up @@ -311,4 +322,4 @@ public void Draw(DrawingContext context, Rect sourceRect, Rect destRect) {
/// <inheritdoc />
public Size Size => ImageImplementation.Size;
}
}
}

0 comments on commit 7c543ab

Please sign in to comment.