webclient异步下载的问题
protected void getTaskList(string keyword, string number)
{
List<Data> list = new List<Data>();
List<ArrayList> arrayListList = new List<ArrayList>();
ArrayList arrayList = new ArrayList();
string path = Application.StartupPath + "\\temp\\" + keyword + "\\";
keyword = HttpUtility.UrlEncode(keyword, Encoding.GetEncoding(936)).ToUpper();
int pn = 0;
while (pn < Int32.Parse(number))
{
string url = @"xxxxxxxxxx";
pn += 20;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream ReceiveStream = response.GetResponseStream())
{
using (StreamReader Reader = new StreamReader(ReceiveStream))
{
string json = Reader.ReadToEnd();
JsonResult Result = JsonConvert.DeserializeObject<JsonResult>(json);
if (Result != null)
{
if (Result.data.Count > 0)
{
list.AddRange(Result.data);
}
}
}
}
}
}
if (list.Count > 0)
{
if (!File.Exists(path)) Directory.CreateDirectory(path);
for (int i = 0; i < list.Count; i++)
{
if (string.IsNullOrEmpty(list[i].objURL)) continue;
arrayList = new ArrayList() { i, list[i].fromPageTitleEnc, list[i].filesize, "0 KB/S", "0%", list[i].objURL, path + Guid.NewGuid().ToString() + "." + list[i].type, true };
arrayListList.Add(arrayList);
}
this.dgvDownLoad.Invoke(new Action<int, object>(this.CallBackMethod), 1, arrayListList);
}
}
这段是我从一个地址获取数据的代码,然后批量下载文件(都是图片文件单个10k-30k不等),一开始下载速度挺快的,每次下载10来个就速度奇慢无比。
void StartDownLoad(object o)
{
SynFileInfo m_SynFileInfo = (SynFileInfo)o;
m_SynFileInfo.LastTime = DateTime.Now;
//再次new 避免WebClient不能I/O并发
WebClient client = new WebClient();
if (m_SynFileInfo.Async)
{
//异步下载
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri(m_SynFileInfo.DownPath), m_SynFileInfo.SavePath, m_SynFileInfo);
}
else client.DownloadFile(new Uri(m_SynFileInfo.DownPath), m_SynFileInfo.SavePath);
}
这里是异步方法,请问是什么原因呢? --------------------编程问答-------------------- 求助!!求助!!求助!!求助!!
补充:.NET技术 , C#