Last active
May 31, 2021 13:49
-
-
Save glebov21/a8a110dc759e83e66966ee48dbcdedcd to your computer and use it in GitHub Desktop.
.net 4.0 download async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Calibration { | |
public static class Server { | |
public delegate void ProgressDelegate(int percents, long bytesReceived, long totalBytesToReceive); | |
public static Task<string> DownloadFileAsync(string path, string saveFilePath, ProgressDelegate onProgress = null) | |
{ | |
return DownloadAsync((webClient) => | |
{ | |
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>(path); | |
webClient.DownloadFileCompleted += (sender, e) => | |
{ | |
if (e.Error != null) | |
tcs.TrySetException(e.Error); | |
else if (e.Cancelled) | |
tcs.TrySetCanceled(); | |
else | |
tcs.TrySetResult(saveFilePath); | |
}; | |
webClient.DownloadFileAsync(new Uri(path), saveFilePath); | |
return tcs.Task; | |
}, onProgress); | |
} | |
public static Task<byte[]> DownloadDataAsync(string path, ProgressDelegate onProgress = null) | |
{ | |
return DownloadAsync((webClient) => | |
{ | |
TaskCompletionSource<byte[]> tcs = new TaskCompletionSource<byte[]>(path); | |
webClient.DownloadDataCompleted += (sender, e) => | |
{ | |
if (e.Error != null) | |
tcs.TrySetException(e.Error); | |
else if (e.Cancelled) | |
tcs.TrySetCanceled(); | |
else | |
tcs.TrySetResult(e.Result); | |
}; | |
webClient.DownloadDataAsync(new Uri(path)); | |
return tcs.Task; | |
}, onProgress); | |
} | |
public static Task<string> DownloadStringAsync(string path, ProgressDelegate onProgress = null) | |
{ | |
return DownloadAsync((webClient) => | |
{ | |
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>(path); | |
webClient.DownloadStringCompleted += (sender, e) => | |
{ | |
if (e.Error != null) | |
tcs.TrySetException(e.Error); | |
else if (e.Cancelled) | |
tcs.TrySetCanceled(); | |
else | |
tcs.TrySetResult(e.Result); | |
}; | |
webClient.DownloadStringAsync(new Uri(path)); | |
return tcs.Task; | |
}, onProgress); | |
} | |
private static Task<T> DownloadAsync<T>(Func<WebClient, Task<T>> downloadAction, ProgressDelegate onProgress = null) | |
{ | |
using (var webClient = new WebClient()) | |
{ | |
if (onProgress != null) | |
{ | |
webClient.DownloadProgressChanged += (sender, e) => | |
{ | |
SynchronizationContext.Current.Post((state) => | |
{ | |
onProgress.Invoke(e.ProgressPercentage, e.BytesReceived, e.TotalBytesToReceive); | |
}, null); | |
}; | |
} | |
webClient.Encoding = Encoding.UTF8; | |
return downloadAction(webClient); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment