简体   繁体   中英

DownloadFileAsync not downloading file

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?

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.");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM