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() protected async Task UpdateImageAsync()
{ {
updateTimer.Stop(); updateTimer.Stop();
cancellationTokenSource?.Cancel(); cancellationTokenSource?.Cancel();
ClearValue(LoadingProgressProperty);
if (ParentMap != null && ParentMap.ActualWidth > 0d && ParentMap.ActualHeight > 0d) if (ParentMap != null && ParentMap.ActualWidth > 0d && ParentMap.ActualHeight > 0d)
{ {
var width = ParentMap.ActualWidth * RelativeImageSize; var width = ParentMap.ActualWidth * RelativeImageSize;

View file

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

View file

@ -7,6 +7,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Policy;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WPF #if WPF
@ -102,10 +103,12 @@ namespace MapControl
progress?.Report((double)(tileCount - pendingTiles.Count) / tileCount); 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 try
{ {
var requestCancellationToken = RequestCancellationEnabled ? cancellationToken : CancellationToken.None;
await LoadTileImage(tile, tileSource, cacheName, requestCancellationToken).ConfigureAwait(false); await LoadTileImage(tile, tileSource, cacheName, requestCancellationToken).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)

View file

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

View file

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