简体   繁体   中英

How can i browse using webbrowser to many sites and wait untill finished loading each site document?

private void GetImages()
        {
            for (int i = 0; i < newHtmls.Count; i++)
            {
                uri = newHtmls[i];
                webBrowser1.Navigate(newHtmls[i]);

            }
        }

Then

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (e.Url.ToString() == uri)
        {
            htmlloaded = true;
            MessageBox.Show("fgdsg");
        }
    }

I tried to use htmlloaded flag but not sure how to use.

For example in the metohd GetHtmls in the loop in newHtmls i have 5 links. I want that it will make one loop take the first link navigate only when finished upload the document and do other things in the DocumentCompleted event then back to the loop and get the second link from the newHtmls navigate wait untill doing all things in the event DocumentCompleted and so on.

And i'm calling GetHtmls from another place and each time the List newHtmls have a different number of links inside. And in each time i want to navigate to each link in the list one by one.

Navigate then wait for doing everything in the completed event then navigate to the next link in the list.

The way the code is now it will first loop over all the newHtmls and will try to navigate to all the links before getting to the completed event.

I just used messageBox to see if it's getting there and it does. But later i will do many other things there to each link. Thats why i need to navigate to each link one by one wait for completed to finish everything then navigate to the next link.

What you want is a list of urls waiting to be processed. You GetImages() method should take the first item in the list and process it.

private void GetImages()
{
   uri = urlToProcess.FirstOrDefault();

   if (uri != null) // null if there or no more to process
   {
       webBrowser1.Navigate(uri);
   }
}

Once the web browser has navigate to the screen webBrowser1_DocumentCompleted will be called so you need to; do what ever your other requires, then remove the url from the list and call GetImage() again.

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url.ToString() == uri)
    {
        htmlloaded = true;
        MessageBox.Show("fgdsg");
    }

    urlsToProcess.RemoveAt(0);
    GetImages();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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