From cbe2751db7e01d29934ac4209113bb4fadf3f2cd Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Sat, 31 Aug 2024 09:11:15 +0200 Subject: [PATCH] Update TileImageLoader.cs --- MapControl/Shared/TileImageLoader.cs | 31 +++++++--------------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index 981aa610..81af077e 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -25,25 +25,6 @@ namespace MapControl public partial class TileImageLoader : ITileImageLoader { - private class TileQueue : ConcurrentStack - { - public TileQueue(IEnumerable tiles) - : base(tiles.Where(tile => tile.IsPending).Reverse()) - { - } - - public bool TryDequeue(out Tile tile) - { - if (!TryPop(out tile)) - { - return false; - } - - tile.IsPending = false; - return true; - } - } - /// /// Default folder path where a persistent cache implementation may save data, i.e. "C:\ProgramData\MapControl\TileCache". /// @@ -74,7 +55,7 @@ namespace MapControl public static int MaxLoadTasks { get; set; } = 4; - private TileQueue pendingTiles; + private ConcurrentStack pendingTiles; /// /// Loads all pending tiles from the tiles collection. @@ -87,7 +68,7 @@ namespace MapControl if (tiles != null && tileSource != null) { - pendingTiles = new TileQueue(tiles); + pendingTiles = new ConcurrentStack(tiles.Where(tile => tile.IsPending).Reverse()); var tileCount = pendingTiles.Count; var taskCount = Math.Min(tileCount, MaxLoadTasks); @@ -101,14 +82,16 @@ namespace MapControl progress?.Report(0d); - var tileQueue = pendingTiles; // pendingTiles may change while tasks are running var tasks = new Task[taskCount]; + var tileStack = pendingTiles; // pendingTiles member may change while tasks are running async Task LoadTilesFromQueueAsync() { - while (tileQueue.TryDequeue(out var tile)) + while (tileStack.TryPop(out var tile)) // use captured tileStack variable in local function { - progress?.Report((double)(tileCount - tileQueue.Count) / tileCount); + tile.IsPending = false; + + progress?.Report((double)(tileCount - tileStack.Count) / tileCount); await LoadTileAsync(tile, tileSource, cacheName).ConfigureAwait(false); }