C# WebBrowserDocumentCompletedEventHandler inside of foreach loop
How do I cause the documentCompleted event to actually wait for completion inside a foreach loop.
The StaticStringclass.URLList is a list of websites, so just like www.google.com,www.aol.com.
Any advise is awesome.
StaticStringClass.holderList = new Queue();
StaticStringClass.QueryHolder = new List<string>();
StaticStringClass.CrawledBit = new List<string>();
StaticStringClass.URLList = new List<string>();
string startingHTML = "http://www.decodethis.com/Default.aspx?tabid=65&vin=";
foreach (string listCar in StaticStringClass.CarIDs)
{
StaticStringClass.CarLister = listCar;
string realModel = string.Empty;
string realTrim = string.Empty;
string htmlHold = string.Empty;
string[] splitListCar = listCar.Split('|');
string realvin = splitListCar[1];
StaticStringClass.URLList.Add(startingHTML + realvin);
}
ProcessSites();
}
private Queue<string> downloadQueue = new Queue<string>();
public void ProcessSite开发者_运维知识库s()
{
foreach (string siteList in StaticStringClass.URLList)
downloadQueue.Enqueue(siteList);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
if (downloadQueue.Count > 0)
{
string nextSite = downloadQueue.Dequeue();
webBrowser1.Navigate(nextSite);
}
//foreach (string siteList in StaticStringClass.URLList)
//{
// webBrowser1.Navigate(siteList);
// webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
//}
}
This approach will not work. Navigate()
is an asynchronous operation - it will not have completed by the time you go to the next siteList
in your foreach
loop.
What is your use case here? If you are just trying to download these sites, use a WebClient
instead or if you need to process the HTML use the HtmlAgilityPack:
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://google.com");
var allDivs = doc.DocumentNode.Descendants("div");
Otherwise you could chain your site processing by using a download queue: In each completed event handler you do your regular processing then check if there are more sites to download, if so de-queue a site and call Navigate()
again with the new site - rinse and repeat, i.e.:
private Queue<string> downloadQueue = new Queue<string>();
public void ProcessSites()
{
foreach (string siteList in StaticStringClass.URLList)
downloadQueue.Enqueue(siteList);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
if (downloadQueue.Count > 0)
{
string nextSite = downloadQueue.Dequeue();
webBrowser1.Navigate(nextSite);
}
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//other processing here
if(downloadQueue.Count > 0)
{
string nextSite = downloadQueue.Dequeue();
webBrowser1.Navigate(nextSite);
}
}
精彩评论