これは、私が行ったすべての変更後の新しいクラス コードです。
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;
namespace WeatherMaps
{
class ExtractImages
{
int count = 0;
int length;
string stringForSatelliteMapUrls = "http://www.sat24.com/";
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);
}
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"))
{
downloadQueue.Enqueue(
new DownloadData(
new Uri(imageSatelliteUrl),
UrlsDir + "SatelliteImage" + i.ToString("D6")
)
);
}
}
}
}
public class DownloadData
{
public Uri DownloadUri;
public string TargetPath;
public DownloadData(Uri downloadUri, string targetPath)
{
this.DownloadUri = downloadUri;
this.TargetPath = targetPath;
}
}
変数リストには、文字列「グループ」のインデックスがいくつかあるため、このフィルターを追加する必要がありました。また、imageSatelliteUrl リストのリンクは http:// で始まっていないため、このフィルターも追加しましたが、良い方法かどうかはわかりません。
Form1で私がした:
Form1 の上部で、次のことを行いました。
private WebClient _webClient = null;
private readonly Queue<ExtractImages.DownloadData> _downloadQueue = new Queue<ExtractImages.DownloadData>();
ExtractImages ei;
Form1 のコンストラクターで、次のことを行いました。
InitializeWebClient();
次に、Form1 の backgroundworker の DoWork イベントで次のことを行いました。
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();
pb.Tag = dd;
_webClient.DownloadFileAsync(
dd.DownloadUri,
dd.TargetPath,
pb
);
if (_downloadQueue.Count == 0) break;
}
}
}
}
次に、Form1 に次のメソッドを追加しました。
private void InitializeWebClient()
{
_webClient = new WebClient();
_webClient.DownloadFileCompleted += DownloadCompletedCallback;
_webClient.DownloadProgressChanged += DownloadProgressCallback;
}
最後に、次の 2 つのイベントを追加しました。
private void DownloadCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
//... download cancelled...
}
else if (e.Error != null)
{
//... download failed...
}
ProgressBar pb = e.UserState as ProgressBar;
lock (_downloadQueue)
{
if (_downloadQueue.Count == 0)
{
if (pb != null) pb.Tag = null;
}
else
{
ExtractImages.DownloadData dd = _downloadQueue.Dequeue();
if (pb != null) pb.Tag = dd;
_webClient.DownloadFileAsync(
dd.DownloadUri,
dd.TargetPath,
e.UserState
);
}
}
}
private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
ProgressBar pb = e.UserState as ProgressBar;
if (pb != null) pb.Value = e.ProgressPercentage;
}
しかし、ブレークポイントを使用して1つのファイルをダウンロードすると、ダウンロードが停止するか、ダウンロードが続行されず、プログレスバーに何も表示されません。
編集**
現在の ExtractImages メソッド:
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));
counter++;
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);
}
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"))
{
downloadQueue.Enqueue(
new DownloadData(
new Uri(imageSatelliteUrl),
UrlsDir + "SatelliteImage" + counter.ToString("D6")
)
);
}
}
}
}