在BackgroundWorker中重复使用DownloadFile的麻烦 [英] Trouble reusing DownloadFile inside a BackgroundWorker

查看:135
本文介绍了在BackgroundWorker中重复使用DownloadFile的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的课程:

  public class DownloadFile 
{
...

public string GetFile(string fileUrl,string pathOut)
{
using(WebClient wc = new WebClient())
{
wc.DownloadFile(new Uri (fileUrl),pathOut);
return pathOut;
}
}
}

BackgroundWorker 2次下载和安装2个文件(在这里执行自定义安装程序)。



问题是第一个文件正常下载并安装,但第二个文件挂在 wc2.DownloadFile(new Uri(fileUrl),pathOut); 行和从不使用使用我正在处理 WebClient 每次我使用,所以我可以说:

  //创建BackgroundWorker所以UI不不能被阻止,我可以
//可以显示日志中的进度...
BackgroundWorker bkWrk = new BackgroundWorker();
bkWrk.WorkerReportsProgress = true;
bkWrk.ProgressChanged + =(s,e)=>
{
ReportProgress(String.Format(Progress:{0}%,e.ProgressPercentage));
};
bkWrk.DoWork = delegate {

DownloadFile fileManager = new DownloadFile();

fileManager.GetFile(http://domain.com/file_A.zip,C:\\\TEMP\\file_a.zip);
fileManager.GetFile(http://domain.com/file_B.zip,C:\\\TEMP\\file_b.zip);
};
bkWrk.RunWorkerAsync();

while(bkWrk.IsBusy)
{
//让我们等待但是触发所有事件
Application.DoEvents();
}

我没有看到任何问题,但事实是,文件挂在 DownloadFile 方法上,甚至尝试使用Microsoft符号在方法内导航,没有运气。



甚至添加了一个标题的请求,但仍然,问题仍然是

  wc.Headers [User-Agent] = Mozilla / 4.0(兼容; Windows NT 5.1; MSIE 6.0)+ 
(兼容; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727);

我在这里缺少一些明显的东西?

解决方案


DownloadFile fileManager = new DownloadFile();
fileManager.GetFile(http://domain.com/file_A.zip,
C:\TEMP\file_a.zip);
fileManager.GetFile(http://domain.com/file_B.zip,
C:\TEMP\file_b.zip);


< blockquote>

你明白wc.DownloadFile不是Async方法吗?这意味着您的后台工作人员必须先下载第一个文件,然后下载第二个文件,然后才能完成工作。


更大的问题是,有时它下载两个文件
没有问题: - / -


这只是意味着你等待了足以让两个动作完成。


I have a simple class:

public class DownloadFile
{
    ...

    public string GetFile(string fileUrl, string pathOut)
    {
        using (WebClient wc = new WebClient())
        {
            wc.DownloadFile(new Uri(fileUrl), pathOut);
            return pathOut;
        }
    }
}

that I call it from a BackgroundWorker 2 times as the process is to download and install 2 files (Doing a custom Installer here).

Problem is that, the first file downloads and installs normally, but the 2nd file hangs on the wc2.DownloadFile(new Uri(fileUrl), pathOut); line and never get's out from there!

using using I'm disposing the WebClient every time I use, so I could say:

// Created BackgroundWorker so the UI doesn't get blocked and I can
//   can show the progress in a log...
BackgroundWorker bkWrk = new BackgroundWorker();
bkWrk.WorkerReportsProgress = true;
bkWrk.ProgressChanged += (s, e) =>
{
    ReportProgress(String.Format("Progress: {0}%", e.ProgressPercentage));
};
bkWrk.DoWork = delegate {

    DownloadFile fileManager = new DownloadFile();

    fileManager.GetFile("http://domain.com/file_A.zip", "C:\\TEMP\\file_a.zip");
    fileManager.GetFile("http://domain.com/file_B.zip", "C:\\TEMP\\file_b.zip");
};
bkWrk.RunWorkerAsync();

while(bkWrk.IsBusy)
{
    // let's wait but fire all events 
    Application.DoEvents();
}

I do not see any problem here ... but the truth is that the file hangs on the DownloadFile method, even tried using the Microsoft Symbols to navigate inside the method with no luck.

Even added a header to the request, but still, the problem remains

wc.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)" +
                           " (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

Am I missing something obvious here?

解决方案

DownloadFile fileManager = new DownloadFile(); fileManager.GetFile("http://domain.com/file_A.zip", "C:\TEMP\file_a.zip"); fileManager.GetFile("http://domain.com/file_B.zip", "C:\TEMP\file_b.zip");

You do understand that wc.DownloadFile isn't an Async method right? This means your background worker must download the first file, then the second file, before its work has been completed.

it does ... bigger problem, is that sometimes it download both files without a problem :-/ –

This just means you waited long enough for both actions to be completed.

这篇关于在BackgroundWorker中重复使用DownloadFile的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆