Skip to content

Commit

Permalink
Fix "CancellationTokenSource has been disposed" when redrawn rapidly …
Browse files Browse the repository at this point in the history
…in virtualizing panels. #19
  • Loading branch information
SKProCH committed Sep 23, 2023
1 parent 830e2bf commit 1102e6e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,22 @@ private void ClearSourceIfUserProvideImage() {

private async void UpdateImage(string? source, IAsyncImageLoader? loader)
{
_updateCancellationToken?.Cancel();
_updateCancellationToken?.Dispose();
_updateCancellationToken = null;
var cancellationTokenSource = new CancellationTokenSource();

var oldCancellationToken = Interlocked.Exchange(ref _updateCancellationToken, cancellationTokenSource);
oldCancellationToken?.Cancel();
oldCancellationToken?.Dispose();

if (source is null && CurrentImage is not ImageWrapper) {
// User provided image himself
return;
}

var cancellationTokenSource = _updateCancellationToken = new CancellationTokenSource();
IsLoading = true;
CurrentImage = null;


Bitmap? bitmap = await Task.Run(async () =>
var bitmap = await Task.Run(async () =>
{
try
{
Expand Down Expand Up @@ -230,7 +232,7 @@ private async void UpdateImage(string? source, IAsyncImageLoader? loader)
{
return null;
}
});
}, CancellationToken.None);

if (cancellationTokenSource.IsCancellationRequested)
return;
Expand Down
4 changes: 2 additions & 2 deletions AsyncImageLoader.Avalonia/AsyncImageLoader.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<RepositoryType>git</RepositoryType>
<PackageTags>image cross-platform avalonia avaloniaui c-sharp-library</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>3.2.0</Version>
<Version>3.2.1-rc1</Version>
<PackageReleaseNotes>
- Add support for existing IImage loading in AdvancedImage
- Tries to fix "CancellationTokenSource has been disposed" when redrawn rapidly in virtualizing panels
</PackageReleaseNotes>
</PropertyGroup>

Expand Down

0 comments on commit 1102e6e

Please sign in to comment.