这是我做了所有更改后的新类代码:
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 中后台工作人员的 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;
}
最后添加了这两个事件:
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;
}
但它所做的是下载一个我使用断点的文件,然后它停止或不继续下载,我在进度条中看不到任何东西。
编辑**
方法 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")
)
);
}
}
}
}