简体   繁体   English

我正在使用Webclient在backgroundworker的“ DoWork”中下载文件

[英]I'm using a webclient to download files inside a backgroundworker's “DoWork”

I could use only the web client I guess. 我猜只能使用Web客户端。

But now I'm using a BackgroundWorker thread and the web client for downloading files. 但是现在我正在使用BackgroundWorker线程和Web客户端来下载文件。

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    var v = lines.Where(s => s.Contains("Name")).Select(s => s.Substring(15));
    var q = lines.Where(s => s.Contains("Code")).Select(s => s.Substring(15));
    var r = q.Where(c => c == "is").Concat(q.Where(c => c != "is"));
    var p = v.Where(c => c == "Israel").Concat(v.Where(c => c != "Israel"));

    var n = r.Count();
    int i = 0;

    var results = p.ToList();

    using (var client = new WebClient())
    {
        foreach (var c in r)
        {
            string filesPath = defaultPath + "\\Countries" + "\\" + results[i] + "\\" + results[i] + ".gif";
            Uri uri = new Uri("http://api.sat24.com/animated/" + c + "/infraPolair/1/JerusalemStandardTime/1897199");
            client.DownloadFile(uri, filesPath);
            backgroundWorker1.ReportProgress(i * 100 / n, results[i]);
            ++i;
        }
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    try
    {
        progressBar1.Value = e.ProgressPercentage;
        label1.Text = e.ProgressPercentage.ToString() + "%";
        label2.Text = e.UserState.ToString();
    }
    catch (Exception ex)
    {
        string ttt = ex.ToString();
    }
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error == null)
    {
        progressBar1.Value = 100;
    }
    else
    {
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    DirectoryInfo dir1 = new DirectoryInfo(@"C:\Users\Chocolade\AppData\Local\SatellitesImagesDownloads\SatellitesImagesDownloads\Countries\");
    fi = dir1.GetFiles("*.gif", SearchOption.AllDirectories);

    foreach (FileInfo finfo in fi)
    {
        if (fi.Length > 0 && finfo.Length > 0)
        {
            timer1.Enabled = false;
            pictureBox1.Load(finfo.FullName);

            listView1.Items[0].Checked = true;
        }
    }
}

The first problem as I asked yesterday was about the timer1 tick event. 我昨天问的第一个问题是关于timer1滴答事件。

But maybe I can use some other way then a timer to check in real time the sub directories? 但是也许我可以使用其他方法然后使用计时器来实时检查子目录?

The idea here is to check all the time every 100ms in the timer tick event the main directory and all sub directories for gif files. 这里的想法是在计时器滴答事件中每100ms一直检查一次gif文件的主目录和所有子目录。

The rule is first check and find the Israel.gif file display it in pictureBox1 and set the checkbox to true. 规则是首先检查并找到Israel.gif文件,将其显示在pictureBox1中,然后将复选框设置为true。

Then after that continue and find all sub directories gif files and set the checkboxes near them to true. 然后,继续,找到所有子目录gif文件,并将它们附近的复选框设置为true。 Don't show the other gifs in the pictureBox1 only the Israel.gif 不要在pictureBox1中显示其他的gif,而只显示Israel.gif

Why I'm asking this way? 为什么我这样问? Because I wanted to display maybe a progressBar or some other indication too when each gif is downloaded. 因为下载每个gif时我也想显示一个progressBar或其他指示。 But the way I'm using the BackgroundWorker and the web client - how can I check when a gif is downloaded completely? 但是,我使用BackgroundWorker和Web客户端的方式-如何检查gif的下载时间? I can check and I'm using a progressBar for overall process - but per gif download? 我可以检查,并且我使用了一个ProgressBar进行整体处理-但是否按gif下载?

That's why I'm using the timer1. 这就是为什么我使用timer1的原因。

You may change your approach: 您可以更改方法:

  • keep the timer, 保持计时器,
  • don't use a backgroundworker, 不要使用背景工作人员,
  • execute the files download in a secondary thread. 在辅助线程中执行文件下载。

In the secondary thread, store somewhere (in variables of the form) the number of files downloaded and the name of the file currently downloaded + any additional info you want to display. 在辅助线程中,将下载的文件数和当前下载的文件名以及要显示的任何其他信息存储在某个地方(以表格的变量形式)。

Use the timer to increment your progressbar, to display the file in progress + other info. 使用计时器增加进度条,以显示正在进行的文件和其他信息。

You don't need to browse your directory: all info is set in variables by the secondary thread . 您无需浏览目录:所有信息都由辅助线程在变量中设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 backgroundworker dowork 事件中使用 WebClient progresschanged 事件? - How do I use the WebClient progresschanged event from within backgroundworker dowork event? WPF将TreeView传递给背景工作人员的DoWork方法 - WPF Pass TreeView to a backgroundworker's DoWork method 为什么在使用 webclient downloadfileasync 下载文件时,progressBar 值出现异常,即它是负值? - Why when downloading files using webclient downloadfileasync I'm getting exception on the progressBar value that it's negative value? 使用XmlReader时BackgroundWorker DoWork委托失败 - BackgroundWorker DoWork delegate fails when using XmlReader 在 BackgroundWorker doWork 事件中使用 Console.WriteLine - Using Console.WriteLine in a BackgroundWorker doWork event 当BackgroundWorker完成传递给DoWork之前,我可以得到DoWorkEventArgs吗? - Can I get DoWorkEventArgs when BackgroundWorker completes before it's passed to DoWork? 如何在Backgroundworker中取消DoWork - how to cancel DoWork in Backgroundworker BackgroundWorker 在 DoWork 之前完成 - BackgroundWorker completes before DoWork 如何使用webclient和线程下载多个文件? - How to download multiple files using webclient and threading? 在 Unity 中使用 WebClient 下载文件时遇到问题 - Having Issues Using WebClient to Download Files In Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM