WebClient.DownloadProgressChanged永远不会被调用 [英] WebClient.DownloadProgressChanged never gets called
问题描述
我在WebClient的 DownloadProgressChanged
事件中添加了一个事件处理程序,但它似乎从未触发过。该文件已成功下载,但未更新其进度。
I added an event handler to the WebClient's DownloadProgressChanged
event, but it never seems to fire. The file successfully downloads, but without having its progress updated.
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
,因为我的服务器一直拒绝下载请求,因此我不得不向 WebClient
类添加useragent + cookiecontainer功能。不过,该事件也从未使用 standard WebClient
类触发。所以这不应该是问题。但不管怎么说; 链接到class
I am using WebClient_B
because I had to add useragent+cookiecontainer functionality to the WebClient
class since my server kept rejecting the download request. The event never fired with the 'standard' WebClient
class either, though. So that shouldn't be the problem. But anyway; link to class
推荐答案
client.DownloadFile(url, savepath);
您必须使用异步版本来下载文件,当前您使用的是阻塞同步版本。
You have to use the async version to download a file, currently you use the blocking, synchronous version.
从 WebClient.DownloadProgressChanged 的 noreferrer> msdn文档:
From the msdn docs for WebClient.DownloadProgressChanged
:
在异步下载操作成功传输
某些或所有数据时发生。
Occurs when an asynchronous download operation successfully transfers some or all of the data.
在您的情况将是:
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileAsync (url, savepath);
由于您的方法不会直接返回任何结果,因此该重构应该不会有问题,请注意在该方法返回给调用者之前,下载很可能尚未完成。
Since your method does not directly return any result, this refactoring shouldn't be a problem, note though that the download most likely has not been completed by the time the method returns to the caller.
这篇关于WebClient.DownloadProgressChanged永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!