Update TileImageLoader.cs

This commit is contained in:
ClemensF 2021-01-22 23:00:58 +01:00
parent 8139af9cfe
commit 3f396140b6

View file

@ -50,34 +50,26 @@ namespace MapControl
}
private readonly TileQueue tileQueue = new TileQueue();
private Func<Tile, Task> loadTileFunc;
private TileSource tileSource;
private string cacheName;
private int taskCount;
/// <summary>
/// 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).
/// </summary>
public void LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName)
public void LoadTiles(IEnumerable<Tile> 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)
{