简体   繁体   English

foreach循环中的C#WebBrowserDocumentCompletedEventHandler

[英]C# WebBrowserDocumentCompletedEventHandler inside of foreach loop

How do I cause the documentCompleted event to actually wait for completion inside a foreach loop. 如何使documentCompleted事件在foreach循环中实际等待完成。

The StaticStringclass.URLList is a list of websites, so just like www.google.com,www.aol.com. StaticStringclass.URLList是一个网站列表,就像www.google.com,www.aol.com一样。

Any advise is awesome. 任何建议都很棒。

StaticStringClass.holderList = new Queue(); 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 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);
        }





        //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. Navigate()是一个异步操作 - 当你转到foreach循环中的下一个siteList时它就不会完成。

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 : 如果您只是尝试下载这些站点,请使用WebClient或者如果您需要处理HTML,请使用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, ie: 否则,您可以使用下载队列链接您的站点处理:在每个已完成的事件处理程序中,您进行常规处理,然后检查是否有更多站点要下载,如果这样,则为站点Navigate()队列并再次使用新站点调用Navigate() - 冲洗并重复,即:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM