使用WebClient下载文件异步不起作用

Loc*_* Le 3 c# asynchronous webclient download

我正在使用C#在VS15中创建一个控制台应用程序.这是我的下载课程:

class DownloadGamefile
{
    public  void DownloadFile(string address, string location)
    {
        WebClient client = new WebClient();
        Uri Uri = new Uri(address);

        client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
        client.DownloadFileAsync(Uri, location);

    }

    private void DownloadProgress(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 void Completed(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Cancelled == true)
        {
            Console.WriteLine("Download has been canceled.");
        }
        else
        {
            Console.WriteLine("Download completed!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要内容:

class Program
{
    static void Main(string[] args)
    {

        DownloadGamefile DGF = new DownloadGamefile();

        DGF.DownloadFile("URL", @"C:\Users\LocDaiLe\Desktop\file.file");

    }
}
Run Code Online (Sandbox Code Playgroud)

该文件显示在右侧文件夹中,但大小为0字节,我的控制台不显示任何下载进度.

Tom*_*Tom 6

由于异步调用不会阻止当前线程,我的猜测是DownloadFileAsync(https://msdn.microsoft.com/en-us/library/ms144196 ( v=vs.110 ) .aspx)立即返回,导致您的DownloadFile也马上回来.这将在文件完成下载之前结束程序执行.

您需要阻止该线程,直到文件下载完毕.您是否考虑过使用.NET 4.5中的async/await(https://msdn.microsoft.com/en-us/library/hh191443.aspx)?您还可以使用Progress/Completed事件来同步您的线程,以便在文件完成之前应用程序不会退出.Console.ReadLine()也应该阻止当前线程,直到按Enter键.

我扩展了你的代码以支持线程阻塞.这将在检查之间将主线程休眠1秒,直到文件完成.我使用关键字"volatile"(https://msdn.microsoft.com/en-us/library/x13ttww7.aspx)来确保始终抓取最新值.这对多线程很重要._completed = true位于if/else的外部,因为我们希望它即使被取消也要退出.如果需要,您可以扩展此解决方案以处理已取消的下载.

class DownloadGamefile
{
    private volatile bool _completed;

    public void DownloadFile(string address, string location)
    {
        WebClient client = new WebClient();
        Uri Uri = new Uri(address);
        _completed = false;

        client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
        client.DownloadFileAsync(Uri, location);

    }

    public bool DownloadCompleted { get { return _completed; } }

    private void DownloadProgress(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 void Completed(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Cancelled == true)
        {
            Console.WriteLine("Download has been canceled.");
        }
        else
        {
            Console.WriteLine("Download completed!");
        }

        _completed = true;
    }
}

class Program
{
    static void Main(string[] args)
    {

        DownloadGamefile DGF = new DownloadGamefile();

        DGF.DownloadFile("URL", @"C:\Users\LocDaiLe\Desktop\file.file");

        while (!DGF.DownloadCompleted)
            Thread.Sleep(1000);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,这个线程阻塞方法永远不应该在主线程的UI应用程序中使用.它将使应用程序无效,直到它停止调用Thread.Sleep.如果要在WPF/WinForm中实现此功能,则不需要执行任何线程阻塞,因为与控制台应用程序不同,UI应用程序在用户关闭之前不会尝试退出.返回main方法时,控制台应用程序退出. (2认同)
  • 它也值得研究async/await,它是.NET 4.5中添加的一项出色的新功能,可帮助您与UI同步异步操作.它消除了对Completed事件的需要,因为它将从当前方法返回并在操作完成时返回它返回的位置. (2认同)