I would like to download a file with asynchronous method, in order to show the actual progress of the download to the user.
using System;
using System.Net;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void downloadFile(string url)
{
Object LockObject = new Object();
using (var client = new WebClient())
{
int left = Console.CursorLeft;
int top = Console.CursorTop;
client.DownloadProgressChanged += (o, e) =>
{
lock (LockObject)
{
Console.SetCursorPosition(0, 0);
Console.Write(e.ProgressPercentage + "%");
}
};
client.DownloadFileAsync(new Uri(url), @"c:\asd");
}
}
static void Main(string[] args)
{
Task task = Task.Factory.StartNew(() => downloadFile("http://download.lsoft.net/ActiveDataStudioSetup.exe"));
task.Wait();
//There can be lots of funcs after download......
//!!!
Console.WriteLine("\nIt should be seen ONLY after the full download");
Console.ReadKey();
}
}
}
The download procedure seems to be fine, file is getting and I also see the actual state of that, but after the method called, everything will be executed right after the calling, but the main thread should wait the complete of the download first.