이벤트 처리기를 WebClient의 DownloadProgressChanged
이벤트에 추가했지만 실행되지 않습니다. 파일은 성공적으로 다운로드되지만 진행 상황은 업데이트되지 않습니다.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); }
}
}
출력 : 내 서버가 다운로드 요청을 거절 유지하기 때문에 WebClient
클래스에 사용자 에이전트 + cookiecontainer 기능을 추가했기 때문에
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
클래스로 해고 된 적이 없습니다. 그래서 그게 문제가되어서는 안됩니다. 그러나 어쨌든; link to class
설명이 방법에 의존 할 수 업데이트하는 동안, 동 기적으로 웹 클라이언트를 사용하려면'DownloadProgressChanged'와'DownloadFileCompleted' 핸들러는 모두라는 스레드를 실행 'DownloadDataAsync' (테스트 당). –