async Task<int> GetBytes(string url, CancellationToken cancelToken, IProgress<DownloadBytesProgress> progressIndicator)
{
int receivedBytes = 0;
int totalBytes = -1;
WebClient client = new WebClient();
using (var stream = await client.OpenReadTaskAsync(url))
{
byte[] buffer = new byte[4096];
Int32.TryParse(client.ResponseHeaders[HttpResponseHeader.ContentLength], out totalBytes);
for (;;)
{
int len = await stream.ReadAsync(buffer, 0, buffer.Length);
if (len == 0)
{
await Task.Yield();
break;
}
receivedBytes += len;
cancelToken.ThrowIfCancellationRequested();
if (progressIndicator != null)
{
DownloadBytesProgress args = new DownloadBytesProgress(url, receivedBytes, totalBytes);
progressIndicator.Report(args);
}
}
}
return receivedBytes;
}