mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Version 4.12.2 Added TileQueue.
This commit is contained in:
parent
9bfb663120
commit
3ca73814f1
|
|
@ -21,7 +21,7 @@ namespace MapControl
|
|||
{
|
||||
public interface ITileImageLoader
|
||||
{
|
||||
void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName);
|
||||
void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -391,7 +391,7 @@ namespace MapControl
|
|||
Children.Add(tile.Image);
|
||||
}
|
||||
|
||||
TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName);
|
||||
TileImageLoader.BeginLoadTiles(Tiles, TileSource, SourceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName)
|
||||
public void BeginLoadTiles(IEnumerable<Tile> 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<Tile, Task> 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<Tile, Task> 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);
|
||||
|
|
|
|||
|
|
@ -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<Tile>
|
||||
{
|
||||
private int taskCount;
|
||||
|
||||
public bool Enqueue(IEnumerable<Tile> tiles)
|
||||
public void Enqueue(IEnumerable<Tile> 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<Tile, Task> tileFunc)
|
||||
{
|
||||
var newTasks = Math.Min(Count, maxTasks) - taskCount;
|
||||
|
||||
while (--newTasks >= 0)
|
||||
{
|
||||
Interlocked.Increment(ref taskCount);
|
||||
|
||||
Task.Run(() => DequeueTiles(tileFunc));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DequeueTiles(Func<Tile, Task> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue