mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Reverted image download cancellation
This commit is contained in:
parent
6f82f8012a
commit
2cf8636f4c
|
|
@ -4,7 +4,6 @@ using Avalonia.Media.Imaging;
|
|||
using Avalonia.Platform;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MapControl
|
||||
|
|
@ -39,18 +38,17 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
internal static async Task<IImage> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
internal static async Task<IImage> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
||||
{
|
||||
WriteableBitmap mergedBitmap = null;
|
||||
var p1 = 0d;
|
||||
var p2 = 0d;
|
||||
|
||||
var images = await Task.WhenAll(
|
||||
LoadImageAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); }), cancellationToken),
|
||||
LoadImageAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); }), cancellationToken));
|
||||
LoadImageAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); })),
|
||||
LoadImageAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); })));
|
||||
|
||||
if (!cancellationToken.IsCancellationRequested &&
|
||||
images.Length == 2 &&
|
||||
if (images.Length == 2 &&
|
||||
images[0] is Bitmap bitmap1 &&
|
||||
images[1] is Bitmap bitmap2 &&
|
||||
bitmap1.PixelSize.Height == bitmap2.PixelSize.Height &&
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#if WPF
|
||||
using System.Windows.Media;
|
||||
|
|
@ -28,16 +27,11 @@ namespace MapControl
|
|||
|
||||
static ImageLoader()
|
||||
{
|
||||
HttpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
|
||||
HttpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(30) };
|
||||
HttpClient.DefaultRequestHeaders.Add("User-Agent", $"XAML-Map-Control/{typeof(ImageLoader).Assembly.GetName().Version}");
|
||||
}
|
||||
|
||||
public static Task<ImageSource> LoadImageAsync(Uri uri, IProgress<double> progress = null)
|
||||
{
|
||||
return LoadImageAsync(uri, progress, CancellationToken.None);
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadImageAsync(Uri uri, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
public static async Task<ImageSource> LoadImageAsync(Uri uri, IProgress<double> progress = null)
|
||||
{
|
||||
ImageSource image = null;
|
||||
|
||||
|
|
@ -47,7 +41,7 @@ namespace MapControl
|
|||
{
|
||||
if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
|
||||
{
|
||||
var response = await GetHttpResponseAsync(uri, progress, cancellationToken);
|
||||
var response = await GetHttpResponseAsync(uri, progress);
|
||||
|
||||
if (response?.Buffer != null)
|
||||
{
|
||||
|
|
@ -93,19 +87,26 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
internal static async Task<HttpResponse> GetHttpResponseAsync(Uri uri, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
internal static async Task<HttpResponse> GetHttpResponseAsync(Uri uri, IProgress<double> progress = null)
|
||||
{
|
||||
HttpResponse response = null;
|
||||
|
||||
try
|
||||
{
|
||||
var completionOptions = progress != null ? HttpCompletionOption.ResponseHeadersRead : HttpCompletionOption.ResponseContentRead;
|
||||
|
||||
using (var responseMessage = await HttpClient.GetAsync(uri, completionOptions, cancellationToken).ConfigureAwait(false))
|
||||
using (var responseMessage = await HttpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
|
||||
{
|
||||
if (responseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
byte[] buffer = await responseMessage.Content.ReadAsByteArrayAsync(progress, cancellationToken).ConfigureAwait(false);
|
||||
byte[] buffer;
|
||||
|
||||
if (progress != null && responseMessage.Content.Headers.ContentLength.HasValue)
|
||||
{
|
||||
buffer = await ReadAsByteArray(responseMessage.Content, progress).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
response = new HttpResponse(buffer, responseMessage.Headers.CacheControl?.MaxAge);
|
||||
}
|
||||
|
|
@ -115,17 +116,6 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException ex)
|
||||
{
|
||||
if (ex.CancellationToken.IsCancellationRequested)
|
||||
{
|
||||
Logger?.LogTrace("Cancelled loading image from {uri}", uri);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger?.LogError(ex, "Failed loading image from {uri}", uri);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.LogError(ex, "Failed loading image from {uri}", uri);
|
||||
|
|
@ -133,29 +123,20 @@ namespace MapControl
|
|||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
internal static class HttpContentExtensions
|
||||
{
|
||||
public static async Task<byte[]> ReadAsByteArrayAsync(this HttpContent content, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
{
|
||||
byte[] buffer;
|
||||
|
||||
if (progress != null && content.Headers.ContentLength.HasValue)
|
||||
private static async Task<byte[]> ReadAsByteArray(HttpContent content, IProgress<double> progress)
|
||||
{
|
||||
var length = (int)content.Headers.ContentLength.Value;
|
||||
buffer = new byte[length];
|
||||
var buffer = new byte[length];
|
||||
|
||||
using (var stream = await content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false))
|
||||
using (var stream = await content.ReadAsStreamAsync().ConfigureAwait(false))
|
||||
{
|
||||
int offset = 0;
|
||||
int read;
|
||||
|
||||
while (offset < length &&
|
||||
(read = await stream.ReadAsync(buffer, offset, length - offset, cancellationToken).ConfigureAwait(false)) > 0)
|
||||
(read = await stream.ReadAsync(buffer, offset, length - offset).ConfigureAwait(false)) > 0)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
offset += read;
|
||||
|
||||
if (offset < length) // 1.0 reported by caller
|
||||
|
|
@ -164,29 +145,8 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = await content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#if !NET
|
||||
public static Task<byte[]> ReadAsByteArrayAsync(this HttpContent content, CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
return content.ReadAsByteArrayAsync();
|
||||
}
|
||||
|
||||
public static Task<Stream> ReadAsStreamAsync(this HttpContent content, CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
return content.ReadAsStreamAsync();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#if WPF
|
||||
using System.Windows;
|
||||
|
|
@ -54,7 +53,7 @@ namespace MapControl
|
|||
|
||||
private readonly Progress<double> loadingProgress;
|
||||
private readonly DispatcherTimer updateTimer;
|
||||
private CancellationTokenSource cancellationTokenSource;
|
||||
private bool updateInProgress;
|
||||
|
||||
public MapImageLayer()
|
||||
{
|
||||
|
|
@ -166,15 +165,23 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
protected abstract Task<ImageSource> GetImageAsync(BoundingBox boundingBox, IProgress<double> progress, CancellationToken cancellationToken);
|
||||
protected abstract Task<ImageSource> GetImageAsync(BoundingBox boundingBox, IProgress<double> progress);
|
||||
|
||||
protected async Task UpdateImageAsync()
|
||||
{
|
||||
if (updateInProgress)
|
||||
{
|
||||
// Update image on next tick, start timer if not running.
|
||||
//
|
||||
updateTimer.Run();
|
||||
}
|
||||
else
|
||||
{
|
||||
updateInProgress = true;
|
||||
updateTimer.Stop();
|
||||
|
||||
cancellationTokenSource?.Cancel();
|
||||
|
||||
ClearValue(LoadingProgressProperty);
|
||||
ImageSource image = null;
|
||||
BoundingBox boundingBox = null;
|
||||
|
||||
if (ParentMap != null && ParentMap.ActualWidth > 0d && ParentMap.ActualHeight > 0d)
|
||||
{
|
||||
|
|
@ -183,21 +190,14 @@ namespace MapControl
|
|||
var x = (ParentMap.ActualWidth - width) / 2d;
|
||||
var y = (ParentMap.ActualHeight - height) / 2d;
|
||||
|
||||
var boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(x, y, width, height));
|
||||
boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(x, y, width, height));
|
||||
|
||||
ImageSource image;
|
||||
|
||||
using (cancellationTokenSource = new CancellationTokenSource())
|
||||
{
|
||||
image = await GetImageAsync(boundingBox, loadingProgress, cancellationTokenSource.Token);
|
||||
image = await GetImageAsync(boundingBox, loadingProgress);
|
||||
}
|
||||
|
||||
cancellationTokenSource = null;
|
||||
|
||||
if (image != null && Children.Count >= 2)
|
||||
{
|
||||
SwapImages(image, boundingBox);
|
||||
}
|
||||
|
||||
updateInProgress = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -211,6 +211,8 @@ namespace MapControl
|
|||
}
|
||||
|
||||
private void SwapImages(ImageSource image, BoundingBox boundingBox)
|
||||
{
|
||||
if (Children.Count >= 2)
|
||||
{
|
||||
var topImage = (Image)Children[0];
|
||||
|
||||
|
|
@ -232,3 +234,4 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ namespace MapControl
|
|||
|
||||
if (buffer == null)
|
||||
{
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri, null, CancellationToken.None).ConfigureAwait(false);
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@ using System.Globalization;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
using System.Threading;
|
||||
|
||||
#if WPF
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
|
@ -164,7 +162,7 @@ namespace MapControl
|
|||
/// <summary>
|
||||
/// Loads an ImageSource from the URL returned by GetMapRequestUri().
|
||||
/// </summary>
|
||||
protected override async Task<ImageSource> GetImageAsync(BoundingBox boundingBox, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
protected override async Task<ImageSource> GetImageAsync(BoundingBox boundingBox, IProgress<double> progress)
|
||||
{
|
||||
ImageSource image = null;
|
||||
|
||||
|
|
@ -187,7 +185,7 @@ namespace MapControl
|
|||
|
||||
if (uri != null)
|
||||
{
|
||||
image = await ImageLoader.LoadImageAsync(new Uri(uri), progress, cancellationToken);
|
||||
image = await ImageLoader.LoadImageAsync(new Uri(uri), progress);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -210,7 +208,7 @@ namespace MapControl
|
|||
|
||||
if (uri1 != null && uri2 != null)
|
||||
{
|
||||
image = await ImageLoader.LoadMergedImageAsync(new Uri(uri1), new Uri(uri2), progress, cancellationToken);
|
||||
image = await ImageLoader.LoadMergedImageAsync(new Uri(uri1), new Uri(uri2), progress);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
|
@ -46,18 +45,17 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
||||
{
|
||||
WriteableBitmap mergedBitmap = null;
|
||||
var p1 = 0d;
|
||||
var p2 = 0d;
|
||||
|
||||
var images = await Task.WhenAll(
|
||||
LoadImageAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); }), cancellationToken),
|
||||
LoadImageAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); }), cancellationToken));
|
||||
LoadImageAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); })),
|
||||
LoadImageAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); })));
|
||||
|
||||
if (!cancellationToken.IsCancellationRequested &&
|
||||
images.Length == 2 &&
|
||||
if (images.Length == 2 &&
|
||||
images[0] is BitmapSource bitmap1 &&
|
||||
images[1] is BitmapSource bitmap2 &&
|
||||
bitmap1.PixelHeight == bitmap2.PixelHeight &&
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Graphics.Imaging;
|
||||
using Windows.Storage;
|
||||
|
|
@ -67,7 +66,7 @@ namespace MapControl
|
|||
return image;
|
||||
}
|
||||
|
||||
internal static async Task<WriteableBitmap> LoadWriteableBitmapAsync(Uri uri, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
internal static async Task<WriteableBitmap> LoadWriteableBitmapAsync(Uri uri, IProgress<double> progress)
|
||||
{
|
||||
WriteableBitmap bitmap = null;
|
||||
|
||||
|
|
@ -75,7 +74,7 @@ namespace MapControl
|
|||
|
||||
try
|
||||
{
|
||||
var response = await GetHttpResponseAsync(uri, progress, cancellationToken);
|
||||
var response = await GetHttpResponseAsync(uri, progress);
|
||||
|
||||
if (response?.Buffer != null)
|
||||
{
|
||||
|
|
@ -98,18 +97,17 @@ namespace MapControl
|
|||
return bitmap;
|
||||
}
|
||||
|
||||
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
||||
{
|
||||
WriteableBitmap mergedBitmap = null;
|
||||
var p1 = 0d;
|
||||
var p2 = 0d;
|
||||
|
||||
var bitmaps = await Task.WhenAll(
|
||||
LoadWriteableBitmapAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); }), cancellationToken),
|
||||
LoadWriteableBitmapAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); }), cancellationToken));
|
||||
LoadWriteableBitmapAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); })),
|
||||
LoadWriteableBitmapAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); })));
|
||||
|
||||
if (!cancellationToken.IsCancellationRequested &&
|
||||
bitmaps.Length == 2 &&
|
||||
if (bitmaps.Length == 2 &&
|
||||
bitmaps[0] != null &&
|
||||
bitmaps[1] != null &&
|
||||
bitmaps[0].PixelHeight == bitmaps[1].PixelHeight)
|
||||
|
|
|
|||
Loading…
Reference in a new issue