这是一个新类,下载文件的代码我在那里添加了一个计数器:
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));
}
在新类中,变量计数器是公共静态的。 那么我应该在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;
}
}