lundi 23 février 2015

How to loop over a list and navigate each item to webbrowser document completed event?

For now i'm doing this:



Queue myUrls = new Queue();
List<Uri> UrlList = new List<Uri>();

private void GetHtmls()
{
for (int i = 1; i < 20; i++)
{
adrBarTextBox.Text = sourceUrl + i;
getCurrentBrowser().Navigate(adrBarTextBox.Text);
Uri targetUri = new Uri(sourceUrl + i);
myUrls.Enqueue(targetUri);
}
}


Now i have in Queue variable all the items 19 items. The problem is that it will start to navigate each item first url1 then url2....but in the end it will Navigate only to the last item ur19


And in urislist i hvae 19 items.


Then in the completed document event:



private void Form1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
myUrls.Dequeue();
WebBrowser b = sender as WebBrowser;
//if (b.ReadyState != WebBrowserReadyState.Complete && b.IsBusy == false
if(e.Url.AbsolutePath != b.Url.AbsolutePath)
{
return;
}
else
{
//if (UrlList.Contains(e.Url))
//{
foreach(string st in myUrls)
{
int urlnumber = st.IndexOf("pagenumber=");
string number = st.ToString().Substring(urlnumber + 11);
int num = Int32.Parse(number);
targetHtmls = (combinedHtmlsDir + "\\Html" + num + ".txt");
StreamWriter writer = File.CreateText(targetHtmls);
writer.Write(getCurrentBrowser().DocumentText);
writer.Close();
}
//UrlList.Remove(e.Url);
//}
}
}


I don't want to download the html's. I want to navigate each html Uri address one by one, wait for each uri address to be completed loaded then to create a text file with the Uri content.


In the loop for (int i = 1; i < 45; i++) i need to make somehow that it will make one loop only once navigate then complete the load of the html/page then create the text file in foreach.


When finished go back and in the loop navigate to the next html/page go to the completed event wait for the navigation to completed then create the text file. And so on for all the 19 items.


The problem is that it's navigating only to the last item number 19 since it's doing first all the loop itertions.


And another problem is in the completed event what ever i tried it's not getting into the foreach it keep doing the completed event over again i cant detect find when it finish loading the dosument.


I tried:



if (b.ReadyState != WebBrowserReadyState.Complete && b.IsBusy == false


Then i tried:



if(e.Url.AbsolutePath != b.Url.AbsolutePath)


But it's never get into the foreach it keep doing the completed event ahd when the document loaded it's not getting to the foreach just keep running the program.


Aucun commentaire:

Enregistrer un commentaire