diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index 574d8aa7..2c994856 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -21,7 +21,7 @@ namespace MapControl { public interface ITileImageLoader { - void LoadTilesAsync(IEnumerable tiles, TileSource tileSource, string sourceName); + void BeginLoadTiles(IEnumerable tiles, TileSource tileSource, string sourceName); } /// @@ -391,7 +391,7 @@ namespace MapControl Children.Add(tile.Image); } - TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName); + TileImageLoader.BeginLoadTiles(Tiles, TileSource, SourceName); } } } diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index e59f541b..2bc29a2f 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -4,7 +4,9 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; +using System.Threading; using System.Threading.Tasks; namespace MapControl @@ -43,32 +45,68 @@ namespace MapControl public static string CacheKeyFormat { get; set; } = "{0};{1};{2};{3}{4}"; private readonly TileQueue tileQueue = new TileQueue(); + private int taskCount; /// /// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks. /// If the UriFormat of the TileSource starts with "http" and the sourceName string is non-empty, /// tile images are cached in the TileImageLoader's Cache. /// - public void LoadTilesAsync(IEnumerable tiles, TileSource tileSource, string sourceName) + public void BeginLoadTiles(IEnumerable tiles, TileSource tileSource, string sourceName) { tileQueue.Clear(); - if (tileSource != null && tileQueue.Enqueue(tiles)) + if (tileSource != null) { - if (Cache != null && - tileSource.UriFormat != null && - tileSource.UriFormat.StartsWith("http") && - !string.IsNullOrEmpty(sourceName)) + tileQueue.Enqueue(tiles); + + var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount; + + if (newTasks > 0) { - tileQueue.RunDequeueTasks(MaxLoadTasks, tile => LoadCachedTileImageAsync(tile, tileSource, sourceName)); - } - else - { - tileQueue.RunDequeueTasks(MaxLoadTasks, tile => LoadTileImageAsync(tile, tileSource)); + Func loadTileFunc; + + if (Cache != null && + tileSource.UriFormat != null && + tileSource.UriFormat.StartsWith("http") && + !string.IsNullOrEmpty(sourceName)) + { + loadTileFunc = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName); + } + else + { + loadTileFunc = tile => LoadTileImageAsync(tile, tileSource); + } + + Interlocked.Add(ref taskCount, newTasks); + + while (--newTasks >= 0) + { + Task.Run(() => LoadTilesAsync(loadTileFunc)); + } } } } + private async Task LoadTilesAsync(Func loadFunc) + { + Tile tile; + + while (tileQueue.TryDequeue(out tile)) + { + try + { + await loadFunc(tile); + } + catch (Exception ex) + { + Debug.WriteLine("TileImageLoader: {0}: {1}", tile, ex.Message); + } + } + + Interlocked.Decrement(ref taskCount); + } + private async Task LoadCachedTileImageAsync(Tile tile, TileSource tileSource, string sourceName) { var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel); diff --git a/MapControl/Shared/TileQueue.cs b/MapControl/Shared/TileQueue.cs index ef62c9cf..d3907901 100644 --- a/MapControl/Shared/TileQueue.cs +++ b/MapControl/Shared/TileQueue.cs @@ -2,31 +2,22 @@ // © 2019 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) -using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; -using System.Threading; -using System.Threading.Tasks; namespace MapControl { public class TileQueue : ConcurrentStack { - private int taskCount; - - public bool Enqueue(IEnumerable tiles) + public void Enqueue(IEnumerable tiles) { tiles = tiles.Where(tile => tile.Pending); if (tiles.Any()) { PushRange(tiles.Reverse().ToArray()); - return true; } - - return false; } public bool TryDequeue(out Tile tile) @@ -40,36 +31,5 @@ namespace MapControl return success; } - - public void RunDequeueTasks(int maxTasks, Func tileFunc) - { - var newTasks = Math.Min(Count, maxTasks) - taskCount; - - while (--newTasks >= 0) - { - Interlocked.Increment(ref taskCount); - - Task.Run(() => DequeueTiles(tileFunc)); - } - } - - private async Task DequeueTiles(Func tileFunc) - { - Tile tile; - - while (TryDequeue(out tile)) - { - try - { - await tileFunc(tile); - } - catch (Exception ex) - { - Debug.WriteLine("TileQueue: {0}: {1}", tile, ex.Message); - } - } - - Interlocked.Decrement(ref taskCount); - } } }