mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-01-08 09:40:02 +01:00
Reverted request cancellation in TileImageLoader
This commit is contained in:
parent
00a885b654
commit
775d584df7
|
|
@ -8,18 +8,11 @@ namespace MapControl
|
|||
{
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
private static async Task LoadTileImage(Tile tile, Func<Task<IImage>> loadImageFunc, CancellationToken cancellationToken)
|
||||
private static async Task LoadTileImage(Tile tile, Func<Task<IImage>> loadImageFunc)
|
||||
{
|
||||
var image = await loadImageFunc().ConfigureAwait(false);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
tile.IsPending = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = Dispatcher.UIThread.InvokeAsync(() => tile.SetImageSource(image)); // no need to await InvokeAsync
|
||||
}
|
||||
await Dispatcher.UIThread.InvokeAsync(() => tile.SetImageSource(image));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -165,7 +165,7 @@ namespace MapControl
|
|||
|
||||
foreach (var imageOverlay in imageOverlays)
|
||||
{
|
||||
imageOverlay.ImageSource = await ImageLoader.LoadImageAsync(new Uri(docUri, imageOverlay.ImagePath), null, CancellationToken.None);
|
||||
imageOverlay.ImageSource = await ImageLoader.LoadImageAsync(new Uri(docUri, imageOverlay.ImagePath));
|
||||
}
|
||||
|
||||
return imageOverlays;
|
||||
|
|
|
|||
|
|
@ -32,6 +32,11 @@ namespace MapControl
|
|||
HttpClient.DefaultRequestHeaders.Add("User-Agent", $"XAML-Map-Control/{typeof(ImageLoader).Assembly.GetName().Version}");
|
||||
}
|
||||
|
||||
public static Task<ImageSource> LoadImageAsync(Uri uri, IProgress<double> progress = null)
|
||||
{
|
||||
return LoadImageAsync(uri, progress, CancellationToken.None);
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadImageAsync(Uri uri, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
{
|
||||
ImageSource image = null;
|
||||
|
|
@ -94,7 +99,9 @@ namespace MapControl
|
|||
|
||||
try
|
||||
{
|
||||
using (var responseMessage = await HttpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
|
||||
var completionOptions = progress != null ? HttpCompletionOption.ResponseHeadersRead : HttpCompletionOption.ResponseContentRead;
|
||||
|
||||
using (var responseMessage = await HttpClient.GetAsync(uri, completionOptions, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
if (responseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
#if WPF
|
||||
using System.Windows.Media;
|
||||
using static System.Net.WebRequestMethods;
|
||||
#elif UWP
|
||||
using Windows.UI.Xaml.Media;
|
||||
#elif WINUI
|
||||
|
|
@ -68,13 +67,6 @@ namespace MapControl
|
|||
/// </summary>
|
||||
public static int MaxLoadTasks { get; set; } = 4;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether HTTP requests are cancelled when the LoadTilesAsync method is cancelled.
|
||||
/// If the property value is false, cancellation only stops dequeuing entries from the tile queue,
|
||||
/// but lets currently running requests run to completion.
|
||||
/// </summary>
|
||||
public static bool RequestCancellationEnabled { get; set; }
|
||||
|
||||
private static ILogger logger;
|
||||
private static ILogger Logger => logger ?? (logger = ImageLoader.LoggerFactory?.CreateLogger<TileImageLoader>());
|
||||
|
||||
|
|
@ -103,13 +95,11 @@ namespace MapControl
|
|||
|
||||
progress?.Report((double)(tileCount - pendingTiles.Count) / tileCount);
|
||||
|
||||
Logger?.LogTrace("[{thread}] Load {zoom}/{column}/{row}", Environment.CurrentManagedThreadId, tile.ZoomLevel, tile.Column, tile.Row);
|
||||
Logger?.LogTrace("[{thread}] Loading {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);
|
||||
await LoadTileImage(tile, tileSource, cacheName).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -120,14 +110,7 @@ namespace MapControl
|
|||
|
||||
try
|
||||
{
|
||||
var tasks = new Task[taskCount];
|
||||
|
||||
for (int i = 0; i < taskCount; i++)
|
||||
{
|
||||
tasks[i] = Task.Run(LoadTilesFromQueue, cancellationToken);
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
await Task.WhenAll(Enumerable.Range(0, taskCount).Select(_ => Task.Run(LoadTilesFromQueue, cancellationToken)));
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
|
@ -141,16 +124,16 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
private static async Task LoadTileImage(Tile tile, TileSource tileSource, string cacheName, CancellationToken cancellationToken)
|
||||
private static async Task LoadTileImage(Tile tile, TileSource tileSource, string cacheName)
|
||||
{
|
||||
// Pass tileSource.LoadImageAsync calls to platform-specific method
|
||||
// LoadTileImage(Tile, Func<Task<ImageSource>>) for execution on the UI thread in WinUI and UWP.
|
||||
|
||||
if (string.IsNullOrEmpty(cacheName))
|
||||
{
|
||||
Task<ImageSource> LoadImage() => tileSource.LoadImageAsync(tile.Column, tile.Row, tile.ZoomLevel, cancellationToken);
|
||||
Task<ImageSource> LoadImage() => tileSource.LoadImageAsync(tile.Column, tile.Row, tile.ZoomLevel);
|
||||
|
||||
await LoadTileImage(tile, LoadImage, cancellationToken).ConfigureAwait(false);
|
||||
await LoadTileImage(tile, LoadImage).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -158,19 +141,19 @@ namespace MapControl
|
|||
|
||||
if (uri != null)
|
||||
{
|
||||
var buffer = await LoadCachedBuffer(tile, uri, cacheName, cancellationToken).ConfigureAwait(false);
|
||||
var buffer = await LoadCachedBuffer(tile, uri, cacheName).ConfigureAwait(false);
|
||||
|
||||
if (buffer != null && buffer.Length > 0)
|
||||
{
|
||||
Task<ImageSource> LoadImage() => tileSource.LoadImageAsync(buffer);
|
||||
|
||||
await LoadTileImage(tile, LoadImage, cancellationToken).ConfigureAwait(false);
|
||||
await LoadTileImage(tile, LoadImage).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<byte[]> LoadCachedBuffer(Tile tile, Uri uri, string cacheName, CancellationToken cancellationToken)
|
||||
private static async Task<byte[]> LoadCachedBuffer(Tile tile, Uri uri, string cacheName)
|
||||
{
|
||||
byte[] buffer = null;
|
||||
|
||||
|
|
@ -185,11 +168,7 @@ namespace MapControl
|
|||
|
||||
try
|
||||
{
|
||||
buffer = await Cache.GetAsync(cacheKey, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Logger?.LogTrace("Cancelled Cache.GetAsync({cacheKey})", cacheKey);
|
||||
buffer = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -198,7 +177,7 @@ namespace MapControl
|
|||
|
||||
if (buffer == null)
|
||||
{
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri, null, cancellationToken).ConfigureAwait(false);
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri, null, CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
|
|
@ -215,11 +194,7 @@ namespace MapControl
|
|||
: response.MaxAge.Value
|
||||
};
|
||||
|
||||
await Cache.SetAsync(cacheKey, buffer, options, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Logger?.LogTrace("Cancelled Cache.SetAsync({cacheKey})", cacheKey);
|
||||
await Cache.SetAsync(cacheKey, buffer, options).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -74,11 +74,11 @@ namespace MapControl
|
|||
/// Loads a tile ImageSource asynchronously from GetUri(column, row, zoomLevel).
|
||||
/// This method is called by TileImageLoader when caching is disabled.
|
||||
/// </summary>
|
||||
public virtual Task<ImageSource> LoadImageAsync(int column, int row, int zoomLevel, CancellationToken cancellationToken)
|
||||
public virtual Task<ImageSource> LoadImageAsync(int column, int row, int zoomLevel)
|
||||
{
|
||||
var uri = GetUri(column, row, zoomLevel);
|
||||
|
||||
return uri != null ? ImageLoader.LoadImageAsync(uri, null, cancellationToken) : Task.FromResult((ImageSource)null);
|
||||
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ namespace MapControl
|
|||
{
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
private static async Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc, CancellationToken cancellationToken)
|
||||
private static async Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
|
||||
|
|
@ -16,18 +16,8 @@ namespace MapControl
|
|||
{
|
||||
try
|
||||
{
|
||||
var image = await loadImageFunc();
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
tile.IsPending = true;
|
||||
tcs.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
tile.SetImageSource(image);
|
||||
tcs.TrySetResult(null);
|
||||
}
|
||||
tile.SetImageSource(await loadImageFunc());
|
||||
tcs.TrySetResult(null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -37,7 +27,7 @@ namespace MapControl
|
|||
|
||||
if (!await tile.Image.Dispatcher.TryRunAsync(CoreDispatcherPriority.Low, LoadTileImage))
|
||||
{
|
||||
tcs.TrySetCanceled(CancellationToken.None);
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
|
||||
await tcs.Task;
|
||||
|
|
|
|||
|
|
@ -7,18 +7,11 @@ namespace MapControl
|
|||
{
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
private static async Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc, CancellationToken cancellationToken)
|
||||
private static async Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var image = await loadImageFunc().ConfigureAwait(false);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
tile.IsPending = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = tile.Image.Dispatcher.InvokeAsync(() => tile.SetImageSource(image)); // no need to await InvokeAsync
|
||||
}
|
||||
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImageSource(image));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ namespace MapControl
|
|||
{
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
private static Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc, CancellationToken cancellationToken)
|
||||
private static Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource();
|
||||
|
||||
|
|
@ -16,18 +16,8 @@ namespace MapControl
|
|||
{
|
||||
try
|
||||
{
|
||||
var image = await loadImageFunc();
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
tile.IsPending = true;
|
||||
tcs.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
tile.SetImageSource(image);
|
||||
tcs.TrySetResult();
|
||||
}
|
||||
tile.SetImageSource(await loadImageFunc());
|
||||
tcs.TrySetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -37,7 +27,7 @@ namespace MapControl
|
|||
|
||||
if (!tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, LoadTileImage))
|
||||
{
|
||||
tcs.TrySetCanceled(CancellationToken.None);
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
|
||||
return tcs.Task;
|
||||
|
|
|
|||
Loading…
Reference in a new issue