Improved Task usage

This commit is contained in:
ClemensFischer 2024-08-31 11:55:59 +02:00
parent cbe2751db7
commit 4e753ae718
10 changed files with 30 additions and 39 deletions

View file

@ -149,7 +149,6 @@ namespace MapControl.Caching
public void Refresh(string key)
{
CheckArgument(key);
}
public void Remove(string key)
@ -185,24 +184,28 @@ namespace MapControl.Caching
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
Set(key, value, options);
return Task.CompletedTask;
}
public Task RefreshAsync(string key, CancellationToken token = default)
{
Refresh(key);
return Task.CompletedTask;
}
public Task RemoveAsync(string key, CancellationToken token = default)
{
Remove(key);
return Task.CompletedTask;
}
public Task CleanAsync()
{
Clean();
return Task.CompletedTask;
}

View file

@ -138,13 +138,10 @@ namespace MapControl.Caching
public void Refresh(string key)
{
CheckArgument(key);
}
public Task RefreshAsync(string key, CancellationToken token = default)
{
CheckArgument(key);
return Task.CompletedTask;
}

View file

@ -32,7 +32,7 @@ namespace MapControl
return animation.RunAsync(element);
}
public static async Task SwapOpacities(Control topElement, Control bottomElement)
public static async Task SwapOpacitiesAsync(Control topElement, Control bottomElement)
{
var animation = new Animation
{

View file

@ -230,7 +230,7 @@ namespace MapControl.Caching
public Task CleanAsync()
{
return Task.Factory.StartNew(Clean, TaskCreationOptions.LongRunning);
return Task.Run(Clean);
}
private string GetPath(string key)

View file

@ -244,7 +244,7 @@ namespace MapControl
topImage.Source = image;
SetBoundingBox(topImage, boundingBox);
await OpacityHelper.SwapOpacities(topImage, bottomImage);
await OpacityHelper.SwapOpacitiesAsync(topImage, bottomImage);
}
}
}

View file

@ -113,7 +113,7 @@ namespace MapControl
return finalSize;
}
protected override Task UpdateTileLayer(bool tileSourceChanged)
protected override async Task UpdateTileLayer(bool tileSourceChanged)
{
var updateTiles = false;
@ -142,10 +142,8 @@ namespace MapControl
{
UpdateTiles();
return LoadTiles(Tiles, SourceName);
await LoadTiles(Tiles, SourceName);
}
return Task.CompletedTask;
}
protected override void SetRenderTransform()

View file

@ -62,7 +62,7 @@ namespace MapControl
/// If tileSource.UriFormat starts with "http" and cacheName is a non-empty string,
/// tile images will be cached in the TileImageLoader's Cache - if that is not null.
/// </summary>
public Task LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress)
public async Task LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress)
{
pendingTiles?.Clear();
@ -102,35 +102,33 @@ namespace MapControl
tasks[i] = Task.Run(LoadTilesFromQueueAsync);
}
return Task.WhenAll(tasks);
await Task.WhenAll(tasks);
}
}
return Task.CompletedTask;
}
private static Task LoadTileAsync(Tile tile, TileSource tileSource, string cacheName)
private static async Task LoadTileAsync(Tile tile, TileSource tileSource, string cacheName)
{
try
{
if (string.IsNullOrEmpty(cacheName))
{
return LoadTileAsync(tile, () => tileSource.LoadImageAsync(tile.Column, tile.Row, tile.ZoomLevel));
await LoadTileAsync(tile, () => tileSource.LoadImageAsync(tile.Column, tile.Row, tile.ZoomLevel));
}
var uri = tileSource.GetUri(tile.Column, tile.Row, tile.ZoomLevel);
if (uri != null)
else
{
return LoadCachedTileAsync(tile, uri, cacheName);
var uri = tileSource.GetUri(tile.Column, tile.Row, tile.ZoomLevel);
if (uri != null)
{
await LoadCachedTileAsync(tile, uri, cacheName);
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"TileImageLoader: {tile.ZoomLevel}/{tile.Column}/{tile.Row}: {ex.Message}");
}
return Task.CompletedTask;
}
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheName)
@ -178,7 +176,7 @@ namespace MapControl
}
}
private static Task WriteCacheAsync(string cacheKey, byte[] buffer, TimeSpan? expiration)
private static async Task WriteCacheAsync(string cacheKey, byte[] buffer, TimeSpan? expiration)
{
if (!expiration.HasValue)
{
@ -191,15 +189,13 @@ namespace MapControl
try
{
return Cache.SetAsync(cacheKey,
await Cache.SetAsync(cacheKey,
buffer ?? Array.Empty<byte>(), // cache even if null, when no tile available
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = expiration });
}
catch (Exception ex)
{
Debug.WriteLine($"TileImageLoader.Cache.SetAsync: {cacheKey}: {ex.Message}");
return Task.CompletedTask;
}
}
}

View file

@ -91,7 +91,7 @@ namespace MapControl
return finalSize;
}
protected override Task UpdateTileLayer(bool tileSourceChanged)
protected override async Task UpdateTileLayer(bool tileSourceChanged)
{
// tileSourceChanged is ignored here because it is always false.
@ -100,15 +100,12 @@ namespace MapControl
{
Children.Clear();
return LoadTiles(null, null); // stop TileImageLoader
await LoadTiles(null, null); // stop TileImageLoader
}
if (UpdateChildLayers(tileMatrixSet))
else if (UpdateChildLayers(tileMatrixSet))
{
return LoadTiles(tileMatrixSet);
await LoadTiles(tileMatrixSet);
}
return Task.CompletedTask;
}
protected override void SetRenderTransform()

View file

@ -11,7 +11,7 @@ namespace MapControl
{
public static class OpacityHelper
{
public static async Task SwapOpacities(UIElement topElement, UIElement bottomElement)
public static Task SwapOpacitiesAsync(UIElement topElement, UIElement bottomElement)
{
topElement.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation
{
@ -26,7 +26,7 @@ namespace MapControl
Duration = TimeSpan.Zero
});
await Task.CompletedTask;
return Task.CompletedTask;
}
}
}

View file

@ -26,7 +26,7 @@ namespace MapControl
storyboard.Begin();
}
public static async Task SwapOpacities(UIElement topElement, UIElement bottomElement)
public static Task SwapOpacitiesAsync(UIElement topElement, UIElement bottomElement)
{
BeginOpacityAnimation(topElement, new DoubleAnimation
{
@ -41,7 +41,7 @@ namespace MapControl
Duration = TimeSpan.Zero
});
await Task.CompletedTask;
return Task.CompletedTask;
}
}
}