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
|
public interface ITileImageLoader
|
||||||
{
|
{
|
||||||
void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName);
|
void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -391,7 +391,7 @@ namespace MapControl
|
||||||
Children.Add(tile.Image);
|
Children.Add(tile.Image);
|
||||||
}
|
}
|
||||||
|
|
||||||
TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName);
|
TileImageLoader.BeginLoadTiles(Tiles, TileSource, SourceName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MapControl
|
namespace MapControl
|
||||||
|
|
@ -43,31 +45,67 @@ namespace MapControl
|
||||||
public static string CacheKeyFormat { get; set; } = "{0};{1};{2};{3}{4}";
|
public static string CacheKeyFormat { get; set; } = "{0};{1};{2};{3}{4}";
|
||||||
|
|
||||||
private readonly TileQueue tileQueue = new TileQueue();
|
private readonly TileQueue tileQueue = new TileQueue();
|
||||||
|
private int taskCount;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
|
/// 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,
|
/// 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.
|
/// tile images are cached in the TileImageLoader's Cache.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName)
|
public void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName)
|
||||||
{
|
{
|
||||||
tileQueue.Clear();
|
tileQueue.Clear();
|
||||||
|
|
||||||
if (tileSource != null && tileQueue.Enqueue(tiles))
|
if (tileSource != null)
|
||||||
{
|
{
|
||||||
|
tileQueue.Enqueue(tiles);
|
||||||
|
|
||||||
|
var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;
|
||||||
|
|
||||||
|
if (newTasks > 0)
|
||||||
|
{
|
||||||
|
Func<Tile, Task> loadTileFunc;
|
||||||
|
|
||||||
if (Cache != null &&
|
if (Cache != null &&
|
||||||
tileSource.UriFormat != null &&
|
tileSource.UriFormat != null &&
|
||||||
tileSource.UriFormat.StartsWith("http") &&
|
tileSource.UriFormat.StartsWith("http") &&
|
||||||
!string.IsNullOrEmpty(sourceName))
|
!string.IsNullOrEmpty(sourceName))
|
||||||
{
|
{
|
||||||
tileQueue.RunDequeueTasks(MaxLoadTasks, tile => LoadCachedTileImageAsync(tile, tileSource, sourceName));
|
loadTileFunc = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tileQueue.RunDequeueTasks(MaxLoadTasks, tile => LoadTileImageAsync(tile, tileSource));
|
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)
|
private async Task LoadCachedTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,31 +2,22 @@
|
||||||
// © 2019 Clemens Fischer
|
// © 2019 Clemens Fischer
|
||||||
// Licensed under the Microsoft Public License (Ms-PL)
|
// Licensed under the Microsoft Public License (Ms-PL)
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MapControl
|
namespace MapControl
|
||||||
{
|
{
|
||||||
public class TileQueue : ConcurrentStack<Tile>
|
public class TileQueue : ConcurrentStack<Tile>
|
||||||
{
|
{
|
||||||
private int taskCount;
|
public void Enqueue(IEnumerable<Tile> tiles)
|
||||||
|
|
||||||
public bool Enqueue(IEnumerable<Tile> tiles)
|
|
||||||
{
|
{
|
||||||
tiles = tiles.Where(tile => tile.Pending);
|
tiles = tiles.Where(tile => tile.Pending);
|
||||||
|
|
||||||
if (tiles.Any())
|
if (tiles.Any())
|
||||||
{
|
{
|
||||||
PushRange(tiles.Reverse().ToArray());
|
PushRange(tiles.Reverse().ToArray());
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryDequeue(out Tile tile)
|
public bool TryDequeue(out Tile tile)
|
||||||
|
|
@ -40,36 +31,5 @@ namespace MapControl
|
||||||
|
|
||||||
return success;
|
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