【发布时间】:2013-11-04 05:29:35
【问题描述】:
这是我做了所有更改后的新类代码:
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")
)
);
}
}
}
}
【问题讨论】:
-
WebClient 为此目的提供了 DownloadProgressChanged 事件 - 使用它。请注意,仅当您使用 WebClient 提供的 *Async 方法时,使用此事件才有意义。
-
你的代码现在显示什么?
-
什么都没有。进度条是空的。它只下载新类方法中的文件就是这样。目前我还没有显示任何内容。
-
我将client.DownloadFile改为Async:Uri uri = new Uri(imagesSatelliteUrls[x]); client.DownloadFileAsync(uri, UrlsDir + "SatelliteImage" + x.ToString("D6"));现在我需要将下载文件的进度传递给progressBars。
-
好的,我将用我现在对 webclient 所做的事情来更新我的问题!