From 3f396140b65dda7861be8d0673760a52d5ecb851 Mon Sep 17 00:00:00 2001 From: ClemensF Date: Fri, 22 Jan 2021 23:00:58 +0100 Subject: [PATCH] Update TileImageLoader.cs --- MapControl/Shared/TileImageLoader.cs | 35 +++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index 14a9695b..7571e100 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -50,34 +50,26 @@ namespace MapControl } private readonly TileQueue tileQueue = new TileQueue(); - private Func loadTileFunc; + private TileSource tileSource; + private string cacheName; private int taskCount; /// /// Loads all pending tiles from the tiles collection. - /// If tileSource.UriFormat starts with "http" and cacheName is a non-empty string, + /// If source.UriFormat starts with "http" and cache is a non-empty string, /// tile images will be cached in the TileImageLoader's Cache (if that is not null). /// - public void LoadTiles(IEnumerable tiles, TileSource tileSource, string cacheName) + public void LoadTiles(IEnumerable tiles, TileSource source, string cache) { tileQueue.Clear(); + tileSource = source; + cacheName = Cache != null && (bool)source.UriFormat?.StartsWith("http") ? cache : null; + tiles = tiles.Where(tile => tile.Pending); if (tiles.Any() && tileSource != null) { - if (Cache != null && - tileSource.UriFormat != null && - tileSource.UriFormat.StartsWith("http") && - !string.IsNullOrEmpty(cacheName)) - { - loadTileFunc = tile => LoadCachedTileAsync(tile, tileSource, cacheName); - } - else - { - loadTileFunc = tile => LoadTileAsync(tile, tileSource); - } - tileQueue.Enqueue(tiles); while (taskCount < Math.Min(tileQueue.Count, MaxLoadTasks)) @@ -91,13 +83,24 @@ namespace MapControl private async Task LoadTilesFromQueueAsync() { + // tileSource or cacheName may change after dequeuing a tile + var source = tileSource; + var cache = cacheName; + while (tileQueue.TryDequeue(out Tile tile)) { tile.Pending = false; try { - await loadTileFunc(tile).ConfigureAwait(false); + if (string.IsNullOrEmpty(cache)) + { + await LoadTileAsync(tile, source).ConfigureAwait(false); + } + else + { + await LoadCachedTileAsync(tile, source, cache).ConfigureAwait(false); + } } catch (Exception ex) {