Skip to content

Commit 1102e6e

Browse files
committed
Fix "CancellationTokenSource has been disposed" when redrawn rapidly in virtualizing panels. #19
1 parent 830e2bf commit 1102e6e

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,22 @@ private void ClearSourceIfUserProvideImage() {
186186

187187
private async void UpdateImage(string? source, IAsyncImageLoader? loader)
188188
{
189-
_updateCancellationToken?.Cancel();
190-
_updateCancellationToken?.Dispose();
191-
_updateCancellationToken = null;
189+
var cancellationTokenSource = new CancellationTokenSource();
190+
191+
var oldCancellationToken = Interlocked.Exchange(ref _updateCancellationToken, cancellationTokenSource);
192+
oldCancellationToken?.Cancel();
193+
oldCancellationToken?.Dispose();
194+
192195
if (source is null && CurrentImage is not ImageWrapper) {
193196
// User provided image himself
194197
return;
195198
}
196199

197-
var cancellationTokenSource = _updateCancellationToken = new CancellationTokenSource();
198200
IsLoading = true;
199201
CurrentImage = null;
200202

201203

202-
Bitmap? bitmap = await Task.Run(async () =>
204+
var bitmap = await Task.Run(async () =>
203205
{
204206
try
205207
{
@@ -230,7 +232,7 @@ private async void UpdateImage(string? source, IAsyncImageLoader? loader)
230232
{
231233
return null;
232234
}
233-
});
235+
}, CancellationToken.None);
234236

235237
if (cancellationTokenSource.IsCancellationRequested)
236238
return;

AsyncImageLoader.Avalonia/AsyncImageLoader.Avalonia.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
<RepositoryType>git</RepositoryType>
1616
<PackageTags>image cross-platform avalonia avaloniaui c-sharp-library</PackageTags>
1717
<PackageReadmeFile>README.md</PackageReadmeFile>
18-
<Version>3.2.0</Version>
18+
<Version>3.2.1-rc1</Version>
1919
<PackageReleaseNotes>
20-
- Add support for existing IImage loading in AdvancedImage
20+
- Tries to fix "CancellationTokenSource has been disposed" when redrawn rapidly in virtualizing panels
2121
</PackageReleaseNotes>
2222
</PropertyGroup>
2323

0 commit comments

Comments
 (0)