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.Options;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
|
@ -83,55 +82,43 @@ namespace MapControl
|
|||
/// </summary>
|
||||
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());
|
||||
var tileCount = pendingTiles.Count;
|
||||
var taskCount = Math.Min(tileCount, MaxLoadTasks);
|
||||
using (var semaphore = new SemaphoreSlim(MaxLoadTasks, MaxLoadTasks))
|
||||
{
|
||||
var pendingTiles = tiles.Where(tile => tile.IsPending).ToList();
|
||||
var tileCount = 0;
|
||||
|
||||
if (taskCount > 0)
|
||||
async Task LoadTile(Tile tile)
|
||||
{
|
||||
if (Cache == null || tileSource.UriTemplate == null || !tileSource.UriTemplate.StartsWith("http"))
|
||||
try
|
||||
{
|
||||
cacheName = null; // disable tile image caching
|
||||
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
async Task LoadTilesFromQueueAsync()
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested && pendingTiles.TryPop(out var tile))
|
||||
{
|
||||
tile.IsPending = false;
|
||||
|
||||
progress?.Report((double)(tileCount - pendingTiles.Count) / tileCount);
|
||||
progress?.Report((double)++tileCount / pendingTiles.Count);
|
||||
|
||||
Logger?.LogTrace("[{thread}] Loading tile image {zoom}/{column}/{row}", Environment.CurrentManagedThreadId, tile.ZoomLevel, tile.Column, tile.Row);
|
||||
|
||||
try
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var tasks = new Task[taskCount];
|
||||
|
||||
for (int i = 0; i < taskCount; i++)
|
||||
{
|
||||
tasks[i] = Task.Run(LoadTilesFromQueueAsync, cancellationToken);
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// no action
|
||||
}
|
||||
await Task.WhenAll(pendingTiles.Select(LoadTile));
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue