I'm trying to download a GitHub repository and want to see the progress on the downloaded bytes. This is my code so far:
Task<byte[]> task;
long lastTime = 0;
byte[] contents;
using (var client = new System.Net.Http.HttpClient())
{
var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0}:", githubToken);
credentials = Convert.ToBase64String(System.Text.Encoding.ASCII
.GetBytes(credentials));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers
.AuthenticationHeaderValue("Basic", credentials);
task = client.GetByteArrayAsync(url);
task.ConfigureAwait(false);
while(!task.IsCompleted)
{
if (task.IsFaulted || task.IsCanceled)
break;
else if(timer.ElapsedMilliseconds - (double)lastTime > 0.2)
{
Console.Clear();
Console.WriteLine();// task.Result.Length or something
lastTime = timer.ElapsedMilliseconds;
}
}
contents = task.Result;
}
The problem is I can't just call task.Result.Length
as it would, obviously, halt to await the task completion. GetByteArrayAsync
is also not 'awaitable'. I couldn't find anything so now I was wondering if it is even possible?
Thanks in advance