Image loading with cancellation

This commit is contained in:
ClemensFischer 2025-08-20 19:50:22 +02:00
parent 545e49b306
commit 220597ab31
5 changed files with 14 additions and 8 deletions

View file

@ -171,8 +171,11 @@ namespace MapControl
protected async Task UpdateImageAsync()
{
updateTimer.Stop();
cancellationTokenSource?.Cancel();
ClearValue(LoadingProgressProperty);
if (ParentMap != null && ParentMap.ActualWidth > 0d && ParentMap.ActualHeight > 0d)
{
var width = ParentMap.ActualWidth * RelativeImageSize;

View file

@ -61,8 +61,8 @@ namespace MapControl
private readonly Progress<double> loadingProgress;
private readonly DispatcherTimer updateTimer;
private ITileImageLoader tileImageLoader;
private CancellationTokenSource cancellationTokenSource;
private ITileImageLoader tileImageLoader;
private MapBase parentMap;
protected MapTileLayerBase()

View file

@ -7,6 +7,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Threading;
using System.Threading.Tasks;
#if WPF
@ -102,10 +103,12 @@ namespace MapControl
progress?.Report((double)(tileCount - pendingTiles.Count) / tileCount);
var requestCancellationToken = RequestCancellationEnabled ? cancellationToken : CancellationToken.None;
Logger?.LogTrace("[{thread}] Loading tile image {zoom}/{column}/{row}", Environment.CurrentManagedThreadId, tile.ZoomLevel, tile.Column, tile.Row);
try
{
var requestCancellationToken = RequestCancellationEnabled ? cancellationToken : CancellationToken.None;
await LoadTileImage(tile, tileSource, cacheName, requestCancellationToken).ConfigureAwait(false);
}
catch (Exception ex)

View file

@ -17,16 +17,16 @@ namespace MapControl
try
{
var image = await loadImageFunc();
tcs.TrySetResult(null); // tcs.Task has completed when image is loaded
if (cancellationToken.IsCancellationRequested)
{
tile.IsPending = true;
tcs.TrySetCanceled(cancellationToken);
}
else
{
tile.SetImageSource(image);
tcs.TrySetResult(null);
}
}
catch (Exception ex)
@ -37,7 +37,7 @@ namespace MapControl
if (!await tile.Image.Dispatcher.TryRunAsync(CoreDispatcherPriority.Low, LoadTileImage))
{
tcs.TrySetCanceled(cancellationToken);
tcs.TrySetCanceled(CancellationToken.None);
}
await tcs.Task;

View file

@ -18,15 +18,15 @@ namespace MapControl
{
var image = await loadImageFunc();
tcs.TrySetResult(); // tcs.Task has completed when image is loaded
if (cancellationToken.IsCancellationRequested)
{
tile.IsPending = true;
tcs.TrySetCanceled(cancellationToken);
}
else
{
tile.SetImageSource(image);
tcs.TrySetResult();
}
}
catch (Exception ex)
@ -37,7 +37,7 @@ namespace MapControl
if (!tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, LoadTileImage))
{
tcs.TrySetCanceled(cancellationToken);
tcs.TrySetCanceled(CancellationToken.None);
}
return tcs.Task;