我从网上下载了许多 url,但我不知道所有 url 何时完成下载。与此同时,我写了一些东西,但它看起来太垃圾了,虽然它有效。
在我的代码中,我从dtPP
dataTable 下载所有 url 并将它们保存到matUrlFharsing
数组中。
这是我的代码:
main()
{
Parallel.For(0, dtPP.Rows.Count, i =>
{
string f = "";
WebClient client = new WebClient();
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
lock (dtPP.Rows[i])
{
f = dtPP.Rows[i]["pp_url"].ToString();
}
client.DownloadDataAsync(new Uri(f), i);
});
while (end)
{
}
DoSomething();
}
void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
int h = (int)e.UserState;
page = (int)e.UserState;
myString = enc.GetString(e.Result);
lock (matUrlFharsing[h])
{
lock (dtPP.Rows[h])
{
//save in mat
matUrlFharsing[h] = Pharsing.CreateCorrectHtmlDoc(myString);
}
}
lock (myLocker)
{
ezer = false;
for (int j = 0; j < matUrlFharsing.Length && !ezer; j++)
{
if (matUrlFharsing[j] == "")
ezer = true;
}
end = ezer;
}
}
我可以做些什么来改进或改变它?