mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Update TileImageLoader.cs
This commit is contained in:
parent
da01f8e411
commit
e725be654b
|
|
@ -3,7 +3,6 @@ using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -83,56 +82,44 @@ namespace MapControl
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress, CancellationToken cancellationToken)
|
public async Task LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var pendingTiles = new ConcurrentStack<Tile>(tiles.Where(tile => tile.IsPending).Reverse());
|
using (var semaphore = new SemaphoreSlim(MaxLoadTasks, MaxLoadTasks))
|
||||||
var tileCount = pendingTiles.Count;
|
|
||||||
var taskCount = Math.Min(tileCount, MaxLoadTasks);
|
|
||||||
|
|
||||||
if (taskCount > 0)
|
|
||||||
{
|
{
|
||||||
if (Cache == null || tileSource.UriTemplate == null || !tileSource.UriTemplate.StartsWith("http"))
|
var pendingTiles = tiles.Where(tile => tile.IsPending).ToList();
|
||||||
{
|
var tileCount = 0;
|
||||||
cacheName = null; // disable tile image caching
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task LoadTilesFromQueueAsync()
|
async Task LoadTile(Tile tile)
|
||||||
{
|
{
|
||||||
while (!cancellationToken.IsCancellationRequested && pendingTiles.TryPop(out var tile))
|
try
|
||||||
{
|
{
|
||||||
tile.IsPending = false;
|
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
progress?.Report((double)(tileCount - pendingTiles.Count) / tileCount);
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
Logger?.LogError(ex, "Failed loading tile image {zoom}/{column}/{row}", tile.ZoomLevel, tile.Column, tile.Row);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
catch (OperationCanceledException)
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var tasks = new Task[taskCount];
|
|
||||||
|
|
||||||
for (int i = 0; i < taskCount; i++)
|
|
||||||
{
|
{
|
||||||
tasks[i] = Task.Run(LoadTilesFromQueueAsync, cancellationToken);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.WhenAll(tasks);
|
tile.IsPending = false;
|
||||||
}
|
|
||||||
catch (OperationCanceledException)
|
progress?.Report((double)++tileCount / pendingTiles.Count);
|
||||||
{
|
|
||||||
// no action
|
Logger?.LogTrace("[{thread}] Loading tile image {zoom}/{column}/{row}", Environment.CurrentManagedThreadId, tile.ZoomLevel, tile.Column, tile.Row);
|
||||||
|
|
||||||
|
var requestCancellationToken = RequestCancellationEnabled ? cancellationToken : CancellationToken.None;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await LoadTileImage(tile, tileSource, cacheName, requestCancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger?.LogError(ex, "Failed loading tile image {zoom}/{column}/{row}", tile.ZoomLevel, tile.Column, tile.Row);
|
||||||
|
}
|
||||||
|
|
||||||
|
semaphore.Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await Task.WhenAll(pendingTiles.Select(LoadTile));
|
||||||
|
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
Logger?.LogTrace("Cancelled LoadTilesAsync with {count} pending tiles", pendingTiles.Count);
|
Logger?.LogTrace("Cancelled LoadTilesAsync with {count} pending tiles", pendingTiles.Count);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue