【问题标题】:DownloadFileAsync not downloading fileDownloadFileAsync 不下载文件
【发布时间】:2014-07-17 19:40:16
【问题描述】:

我正在尝试编写从网络下载一个文件的简单应用程序。

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

我在这一行设置了断点:Console.WriteLine("Download complete");,但它从未命中。程序创建空的serverdata.txt 文件。我在控制台中没有收到来自DownloadProgressCallback 的有关下载百分比的更新。我做错了什么?

【问题讨论】:

  • 您的程序在开始下载后立即退出。在下载完成之前,您必须保持它处于活动状态。
  • Console.WriteLine("Download successful.");代替Console.ReadLine();
  • 您使用异步,以便在下载过程中可以做其他事情。退出程序不是您可以做的其他事情之一:)
  • 为什么不尝试在 downloadAsync() 捕获异常并增加超时时间

标签: c#


【解决方案1】:

我还没有尝试过,但您可以尝试使用IsBusy 属性:

while(client.IsBusy)
     Thread.Sleep(1000);

Console.WriteLine("Download successful.");

或者,如果您使用的是 .NET 4.5,请使用 WebClient.DownloadFileTaskAsync 方法

client.DownloadFileTaskAsync(uri, "serverdata.txt").Wait();
Console.WriteLine("Download successful.");

【讨论】:

    【解决方案2】:

    正如其他人使用DownloadFileTaskAsync 所提到的那样,在等待任务完成时让您的生活更轻松。您可以通过await 异步处理结果,也可以调用Wait() 进行阻塞等待。

    代码如下:

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

    Wait() 调用可能会在基于 GUI 的应用程序上死锁,但它适合您的情况。

    【讨论】:

      猜你喜欢
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多