Minor refactoring

This commit is contained in:
ClemensF 2020-10-21 22:18:16 +02:00
parent b719a14d29
commit 5c815e951e
5 changed files with 32 additions and 35 deletions

View file

@ -81,7 +81,7 @@ namespace MapControl
return numIntersections > 0; return numIntersections > 0;
} }
private static bool GetIntersection(ref Point p1, ref Point p2, Point p3, Point p4, Func<Point, bool> condition) private static bool GetIntersection(ref Point p1, ref Point p2, Point p3, Point p4, Predicate<Point> predicate)
{ {
var intersection = GetIntersection(p1, p2, p3, p4); var intersection = GetIntersection(p1, p2, p3, p4);
@ -90,7 +90,7 @@ namespace MapControl
return false; return false;
} }
if (condition(p1)) if (predicate(p1))
{ {
p1 = intersection.Value; p1 = intersection.Value;
} }

View file

@ -36,7 +36,7 @@ namespace MapControl
return item is MapItem; return item is MapItem;
} }
public void SelectItems(Func<object, bool> predicate) public void SelectItems(Predicate<object> predicate)
{ {
if (SelectionMode == SelectionMode.Single) if (SelectionMode == SelectionMode.Single)
{ {
@ -61,7 +61,7 @@ namespace MapControl
} }
} }
public void SelectItemsByLocation(Func<Location, bool> predicate) public void SelectItemsByLocation(Predicate<Location> predicate)
{ {
SelectItems(item => SelectItems(item =>
{ {
@ -70,7 +70,7 @@ namespace MapControl
}); });
} }
public void SelectItemsByPosition(Func<Point, bool> predicate) public void SelectItemsByPosition(Predicate<Point> predicate)
{ {
SelectItems(item => SelectItems(item =>
{ {

View file

@ -51,17 +51,15 @@ namespace MapControl
} }
private readonly TileQueue tileQueue = new TileQueue(); private readonly TileQueue tileQueue = new TileQueue();
private Func<Tile, Task> loadTileImage; private Func<Tile, Task> loadTile;
private int taskCount; private int taskCount;
/// <summary> /// <summary>
/// Loads all pending tiles from the tiles collection. /// Loads all pending tiles from the tiles collection.
/// If tileSource.UriFormat starts with "http" and sourceName is a non-empty string, /// If tileSource.UriFormat starts with "http" and sourceName is a non-empty string,
/// tile images will be cached in the TileImageLoader's Cache (if it's not null). /// tile images will be cached in the TileImageLoader's Cache (if that is not null).
/// The method is async void because it implements void ITileImageLoader.LoadTiles
/// and is not awaited when it is called in MapTileLayer.UpdateTiles().
/// </summary> /// </summary>
public async void LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName) public void LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName)
{ {
tileQueue.Clear(); tileQueue.Clear();
@ -74,45 +72,44 @@ namespace MapControl
tileSource.UriFormat.StartsWith("http") && tileSource.UriFormat.StartsWith("http") &&
!string.IsNullOrEmpty(sourceName)) !string.IsNullOrEmpty(sourceName))
{ {
loadTileImage = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName); loadTile = tile => LoadCachedTileAsync(tile, tileSource, sourceName);
} }
else else
{ {
loadTileImage = tile => LoadTileImageAsync(tile, tileSource); loadTile = tile => LoadTileAsync(tile, tileSource);
} }
tileQueue.Enqueue(tiles); tileQueue.Enqueue(tiles);
var numTasks = Math.Min(tileQueue.Count, MaxLoadTasks); while (taskCount < Math.Min(tileQueue.Count, MaxLoadTasks))
var tasks = Enumerable.Range(0, numTasks).Select(n => LoadTilesFromQueueAsync()); {
Interlocked.Increment(ref taskCount);
await Task.WhenAll(tasks).ConfigureAwait(false); Task.Run(() => LoadTilesFromQueueAsync());
}
} }
} }
private async Task LoadTilesFromQueueAsync() private async Task LoadTilesFromQueueAsync()
{ {
if (Interlocked.Increment(ref taskCount) <= MaxLoadTasks) while (tileQueue.TryDequeue(out Tile tile))
{ {
while (tileQueue.TryDequeue(out Tile tile)) tile.Pending = false;
{
tile.Pending = false;
try try
{ {
await loadTileImage(tile).ConfigureAwait(false); await loadTile(tile).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message); Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
}
} }
} }
Interlocked.Decrement(ref taskCount); Interlocked.Decrement(ref taskCount);
} }
private static async Task LoadCachedTileImageAsync(Tile tile, TileSource tileSource, string sourceName) private static async Task LoadCachedTileAsync(Tile tile, TileSource tileSource, string sourceName)
{ {
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel); var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
@ -127,7 +124,7 @@ namespace MapControl
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension); var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
await LoadCachedTileImageAsync(tile, uri, cacheKey).ConfigureAwait(false); await LoadCachedTileAsync(tile, uri, cacheKey).ConfigureAwait(false);
} }
} }

View file

@ -28,7 +28,7 @@ namespace MapControl
public static Caching.IImageCache Cache { get; set; } public static Caching.IImageCache Cache { get; set; }
private static async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey) private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
{ {
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false); var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
var buffer = cacheItem?.Buffer; var buffer = cacheItem?.Buffer;
@ -51,12 +51,12 @@ namespace MapControl
} }
} }
private static Task LoadTileImageAsync(Tile tile, TileSource tileSource) private static Task LoadTileAsync(Tile tile, TileSource tileSource)
{ {
return SetTileImageAsync(tile, () => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel)); return SetTileImageAsync(tile, () => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
} }
private static async Task SetTileImageAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc) private static async Task SetTileImageAsync(Tile tile, Func<Task<ImageSource>> loadImage)
{ {
var tcs = new TaskCompletionSource<object>(); var tcs = new TaskCompletionSource<object>();
@ -64,7 +64,7 @@ namespace MapControl
{ {
try try
{ {
tile.SetImage(await loadImageFunc()); tile.SetImage(await loadImage());
tcs.SetResult(null); tcs.SetResult(null);
} }
catch (Exception ex) catch (Exception ex)

View file

@ -37,7 +37,7 @@ namespace MapControl
public static ObjectCache Cache { get; set; } = MemoryCache.Default; public static ObjectCache Cache { get; set; } = MemoryCache.Default;
private static async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey) private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
{ {
var cacheItem = await GetCacheAsync(cacheKey).ConfigureAwait(false); var cacheItem = await GetCacheAsync(cacheKey).ConfigureAwait(false);
var buffer = cacheItem?.Buffer; var buffer = cacheItem?.Buffer;
@ -62,7 +62,7 @@ namespace MapControl
} }
} }
private static async Task LoadTileImageAsync(Tile tile, TileSource tileSource) private static async Task LoadTileAsync(Tile tile, TileSource tileSource)
{ {
var image = await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel).ConfigureAwait(false); var image = await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel).ConfigureAwait(false);