1

これは、ファイルをダウンロードする新しいクラスのコードで、そこにカウンターを追加しました。

private void downloadFiles(Queue<DownloadData> downloadQueue, int i, string UrlsDir)
        {
            string imageSatelliteUrl = imagesSatelliteUrls[i];
            if (!imagesSatelliteUrls[i].StartsWith("Group"))
            {
                if (!imagesSatelliteUrls[i].StartsWith("http://"))
                {
                    imagesSatelliteUrls[i] = "http://" + imagesSatelliteUrls[i];
                    imageSatelliteUrl = imagesSatelliteUrls[i];
                }
                if (!imagesSatelliteUrls[i].Contains("href"))
                {
                    counter++;
                    downloadQueue.Enqueue(
                        new DownloadData(
                            new Uri(imageSatelliteUrl),
                            UrlsDir + "SatelliteImage" + counter.ToString("D6")
                        )
                    );
                }
            }
        }

そしてform1には次のコードがあります:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            lock (_downloadQueue)
            {
                ei = new ExtractImages(_downloadQueue, StartTags, LastTags, Maps, localFilename, UrlsPath);

                if (_downloadQueue.Count > 0)
                    foreach (ProgressBar pb in progressbars)
                    {
                        if (pb.Tag == null)
                        {
                            ExtractImages.DownloadData dd = _downloadQueue.Dequeue();
                            StartDownloadWithProgressBar(dd, pb);

                            if (_downloadQueue.Count == 0) break;
                        }
                    }
            }
        }




private void StartDownloadWithProgressBar(ExtractImages.DownloadData downloadData, ProgressBar progressBar)
        {          
                WebClient wc = new WebClient();
                wc.DownloadFileCompleted += DownloadCompletedCallback;
                wc.DownloadProgressChanged += DownloadProgressCallback;

                ActiveDownloadJob adJob = new ActiveDownloadJob(downloadData, progressBar, wc);
                progressBar.Tag = adJob;
                wc.DownloadFileAsync(
                    downloadData.DownloadUri,
                    downloadData.TargetPath,
                    adJob
                );
        }

        class ActiveDownloadJob
        {
            public ExtractImages.DownloadData DownloadData;
            public ProgressBar ProgressBar;
            public WebClient WebClient;

            public ActiveDownloadJob(ExtractImages.DownloadData downloadData, ProgressBar progressBar, WebClient webClient)
            {
                try
                {
                    this.DownloadData = downloadData;
                    this.ProgressBar = progressBar;
                    this.WebClient = webClient;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.ToString());
                }
            }
        }

        private void DownloadCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                //... download cancelled...
            }
            else if (e.Error != null)
            {
                //... download failed...
                Logger.Write("Error: " + e.Error);
            }

            ActiveDownloadJob adJob = e.UserState as ActiveDownloadJob;
            ProgressBar pb = (adJob != null) ? adJob.ProgressBar : null;

            lock (_downloadQueue)
            {
                if (_downloadQueue.Count == 0)
                {
                    if (pb != null) pb.Tag = null;
                }
                else
                {
                    ExtractImages.DownloadData dd = _downloadQueue.Dequeue();
                    StartDownloadWithProgressBar(dd, pb);
                }
            }
        }

        private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
        {
            ActiveDownloadJob adJob = e.UserState as ActiveDownloadJob;
            if (adJob != null && adJob.ProgressBar != null)
                adJob.ProgressBar.Invoke((Action)(() => adJob.ProgressBar.Value = e.ProgressPercentage));

        }

新しいクラスでは、変数 counter は public static です。では、Form1 のラベルのどこにカウンターの進行状況を表示する必要がありますか?

多分私はカウンター変数を必要としませんか?たとえば、「操作が終了しました」というラベルに表示したい場合、操作全体の完了部分はどこにありますか?

コードは少し長いですが、すべて接続されています。

編集**

これは、ダウンロードの新しいクラスの完全なコードです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using unfreez_wrapper;
using System.Drawing;
using System.Globalization;
using HtmlAgilityPack;

namespace WeatherMaps
{


    class ExtractImages
    {
        int count;
        int length;
        string stringForSatelliteMapUrls = "http://www.sat24.com/";
        public static int counter;
        UnFreezWrapper uf;
        List<string> imagesSatelliteUrls;
        List<string> imagesRainUrls;
        string localdir;

        // Instance with one List and Files and Animation
        public ExtractImages(List<string> mapToRead, string LocalFileDir, string UrlsDir)
        {
            //counter = 0;
        }

        // Instance with more then one List and Files and Animation
        public ExtractImages(Queue<DownloadData> downloadQueue, List<string> FirstTags, List<string> LastTags, List<string> Maps, string LocalFileDir, string UrlsDir)
        {
            localdir = LocalFileDir;
            counter = 0;
            imagesSatelliteUrls = new List<string>();
            imagesRainUrls = new List<string>();
            int startIndex = 0;
            int endIndex = 0;
            int position = 0;
            for (int i = 0; i < Maps.Count; i++)
            {
                imagesSatelliteUrls.Add("Group " + (i + 1));
                string startTag = FirstTags[i];
                string endTag = LastTags[i];
                startIndex = Maps[i].IndexOf(startTag);
                while (startIndex > 0)
                {

                    endIndex = Maps[i].IndexOf(endTag, startIndex);
                    if (endIndex == -1)
                    {
                        break;
                    }
                    string t = Maps[i].Substring(startIndex, endIndex - startIndex + endTag.Length);
                    imagesSatelliteUrls.Add(t);
                    position = endIndex + endTag.Length;
                    startIndex = Maps[i].IndexOf(startTag, position);

                    downloadFiles(downloadQueue, i, UrlsDir);
                }
            }
        }

        private void downloadFiles(Queue<DownloadData> downloadQueue, int i, string UrlsDir)
        {
            string imageSatelliteUrl = imagesSatelliteUrls[i];
            if (!imagesSatelliteUrls[i].StartsWith("Group"))
            {
                if (!imagesSatelliteUrls[i].StartsWith("http://"))
                {
                    imagesSatelliteUrls[i] = "http://" + imagesSatelliteUrls[i];
                    imageSatelliteUrl = imagesSatelliteUrls[i];
                }
                if (!imagesSatelliteUrls[i].Contains("href"))
                {
                    counter++;
                    downloadQueue.Enqueue(
                        new DownloadData(
                            new Uri(imageSatelliteUrl),
                            UrlsDir + "SatelliteImage" + counter.ToString("D6")
                        )
                    );
                }
            }
        }

        public class DownloadData
        {
            public Uri DownloadUri;
            public string TargetPath;

            public DownloadData(Uri downloadUri, string targetPath)
            {
                this.DownloadUri = downloadUri;
                this.TargetPath = targetPath;
            }
        }
4

0 に答える 0