简体   繁体   中英

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

I could use only the web client I guess.

But now I'm using a BackgroundWorker thread and the web client for downloading files.

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.

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.

The rule is first check and find the Israel.gif file display it in pictureBox1 and set the checkbox to true.

Then after that continue and find all sub directories gif files and set the checkboxes near them to true. Don't show the other gifs in the pictureBox1 only the 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. But the way I'm using the BackgroundWorker and the web client - how can I check when a gif is downloaded completely? I can check and I'm using a progressBar for overall process - but per gif download?

That's why I'm using the 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM