Reputation: 4496
I'm trying to write simple app which downloads one file from web.
class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();
Uri uri = new Uri("http://download.thinkbroadband.com/100MB.zip");
// Specify that the DownloadFileCallback method gets called
// when the download completes.
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback2);
// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
client.DownloadFileAsync(uri, "serverdata.txt");
Console.WriteLine("Download successful.");
}
private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesReceived,
e.TotalBytesToReceive,
e.ProgressPercentage);
}
private static void DownloadFileCallback2(object sender, AsyncCompletedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("Download complete");
}
}
I put break point in this line: Console.WriteLine("Download complete");
but its never hit. Program creates empty serverdata.txt
file. I receive no updates in console about download % from DownloadProgressCallback
. What I did wrong?
Upvotes: 1
Views: 3627
Reputation: 13495
As men tioned by others using DownloadFileTaskAsync
an make your life easier when it comes to waiting for the completion of the task. You can either await
the result asynchronously or call Wait()
to do a blocking wait.
Here is the code:
private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
((TaskCompletionSource<object>)e.UserState).Task.AsyncState,
e.BytesReceived,
e.TotalBytesToReceive,
e.ProgressPercentage);
}
static void Main(string[] args)
{
WebClient client = new WebClient();
Uri uri = new Uri("http://download.thinkbroadband.com/100MB.zip");
// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
var task = client.DownloadFileTaskAsync(uri, "serverdata.txt"); // use Task based API
task.Wait(); // Wait for download to complete, can deadlock in GUI apps
Console.WriteLine("Download complete");
Console.WriteLine("Download successful.");
}
The Wait()
call can deadlock on a GUI based app, but it's fine for your case.
Upvotes: 1
Reputation: 101701
I haven't tried this but you can try using IsBusy
property :
while(client.IsBusy)
Thread.Sleep(1000);
Console.WriteLine("Download successful.");
OR, use WebClient.DownloadFileTaskAsync
method if you are using .NET 4.5
client.DownloadFileTaskAsync(uri, "serverdata.txt").Wait();
Console.WriteLine("Download successful.");
Upvotes: 8