I download many urls from the web but I don’t know when all the urls have been finished to download. Meanwhile I wrote something, but it seem too me a trash although it works.
In my code I download all the urls from dtPP dataTable and save them to matUrlFharsing array.
Here is my code:
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;
}
}
What can I do to improve or change it?
Small improvement:
Use AutoResetEvent instead of bool for End.