9

我为 WebClient 的DownloadProgressChanged事件添加了一个事件处理程序,但它似乎永远不会触发。文件成功下载,但没有更新其进度。

public class DownloadFile
{
    private File file = null;

    public DownloadFile(File file)
    {
        this.file = file;
    }

    public void startDownloadThread()
    {
        Console.WriteLine("Starting Download : "+file.URL);

        var t = new Thread(() => DownloadThread(file));
        t.Start();
    }

    public Action<string> action_error_downloadFailed = Console.WriteLine;
    private void DownloadThread(File file) //Unnecessary argument but whatever ;D
    {
        try
        {
            string url = file.URL;
            string savepath = file.DestinationDir + "\\" + file.Filename;

            WebClient_B client = new WebClient_B();
            client.Proxy = null; //default to no proxy
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.DownloadFile(url, savepath);

            Console.WriteLine("Download finished :" + file.Filename);
        }
        catch (Exception ex)
        {
            if (action_error_downloadFailed != null)
                action_error_downloadFailed("Download failed :"+ex.Message);
        }
    }

    private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        try
        {
            if (file.TotalSize == 0)
                file.TotalSize = (int)e.TotalBytesToReceive;
            file.CurrentSize = (int)e.BytesReceived;

            Form_DownloadManager.rebuildQueue();

            Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
            (string)e.UserState,
            e.BytesReceived,
            e.TotalBytesToReceive,
            e.ProgressPercentage);
        }
        catch (Exception ex) { Console.WriteLine("client_DownloadProgressChanged error : "+ex.Message); }
    }
}

输出:

Starting Download : http://x.x.x/y/z.zip
'projectname.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread '<No Name>' (0x3b8c) has exited with code 0 (0x0).
Download finished :z.zip

我之所以使用,是因为我的服务器一直拒绝下载请求,WebClient_B因此我必须向该类添加 useragent+cookiecontainer 功能。不过,该事件也从未使用“标准”类WebClient触发。WebClient所以这应该不是问题。但无论如何; 链接到课堂

4

2 回答 2

14
client.DownloadFile(url, savepath);

您必须使用异步版本来下载文件,目前您使用的是阻塞同步版本。

来自msdnWebClient.DownloadProgressChanged文档:

异步下载操作成功传输部分或全部数据时发生。

在你的情况下,这将是:

client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileAsync (url, savepath);

由于您的方法不直接返回任何结果,因此此重构应该不是问题,但请注意,当方法返回给调用者时,下载很可能尚未完成。

于 2012-06-13T13:00:38.177 回答
11

如果你想同步使用 WebClient,在获取进度更新的同时,你可以依赖这里详述的这个方法:

http://alexfeinberg.wordpress.com/2014/09/14/how-to-use-net-webclient-synchronously-and-still-receive-progress-updates/

public void DownloadFile(Uri uri, string destination)
{
  using (var wc = new WebClient())
  {
    wc.DownloadProgressChanged += HandleDownloadProgress;
    wc.DownloadFileCompleted += HandleDownloadComplete;

    var syncObject = new object();
    lock (syncObject)
    {
       wc.DownloadFileAsync(uri, destination, syncObject);
       // This would block the thread until download completes
       Monitor.Wait(syncObject);
    }
  }
 
  // Do more stuff after download was complete
}

private void HandleDownloadComplete(object sender, AsyncCompletedEventArgs args)
{
   lock (args.UserState)
   {  
      // releases blocked thread
      Monitor.Pulse(args.UserState);
   }
}


private void HandleDownloadProgress(object sender, DownloadProgressChangedEventArgs args)
{
  // Process progress updates here
}
于 2014-09-14T15:12:56.087 回答