I'm not new to programming but am new to using WebBrowser controls in C# WinForm apps.
I have two WebBrowser controls that I dynamically load onto a form. The first navigates to a URL and upon completion I loads it's DocumentText into an HtmlAgilityPack document. I use XPath to parse some links that are then passed to the second browser control. Everything works fine until after the second browser control loads. Its .DocumentText is less than 700 bytes long. If I bypass the rest of the routine and return to the screen, the correct and full page is displayed in the second control, however I can't get that to happen inside the routine. The bare bones of the code is as follows.
private WebBrowser webBrowser = new WebBrowser();
private WebBrowser webBrowser2 = new WebBrowser();
private TaskCompletionSource<bool> tcs = null;
private TaskCompletionSource<bool> tcs2 = null;
private string lastnav = "";
private string lastMessage = "";
private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
lastnav = e.Url.ToString();
this.txtNavigated.Text += e.Url.ToString() + "\r\n\r\n";
if (webBrowser.Document != null && webBrowser.Document.Cookie != null)
this.txtNavigated.Text += webBrowser.Document.Cookie + "\r\n\r\n";
this.txtNavigated.Update();
}
private async void TryNavigate(string url)
webBrowser.Location = new System.Drawing.Point(12, 226);
webBrowser.Size = new System.Drawing.Size(1070,100);
webBrowser2.Location = new System.Drawing.Point(12, 327);
webBrowser2.Size = new System.Drawing.Size(1070, 100);
this.Controls.Add(webBrowser);
this.Controls.Add(webBrowser2);
tcs = new TaskCompletionSource<bool>();
WebBrowserDocumentCompletedEventHandler documentCompletedHandler = (sender2, e2) => tcs.TrySetResult(true);
WebBrowserNavigatingEventHandler docNavigatingHandler = webBrowser_Navigating;
try
{
Uri baseUri = new Uri("https://www.labcorp.com/wps/portal/provider/testmenu");
Uri newUri = null;
webBrowser.DocumentCompleted += documentCompletedHandler;
webBrowser.Navigating += docNavigatingHandler;
try
{
webBrowser.Navigate(baseUri.AbsoluteUri);
await tcs.Task;
}
catch (WebException webex) {
lastMessage = webex.Message;
}
catch (Exception ex)
{
lastMessage = ex.Message;
}
finally
{
webBrowser.DocumentCompleted -= documentCompletedHandler;
}
webBrowser2.Navigate("localhost");
webBrowser2.Document.Cookie = webBrowser.Document.Cookie;
webBrowser2.Navigating += docNavigatingHandler;
webBrowser2.DocumentCompleted += documentCompletedHandler2;
HtmlAgilityPack.HtmlDocument azlinks = new HtmlAgilityPack.HtmlDocument();
azlinks.LoadHtml(webBrowser.DocumentText);
// get A - Z
var azlinkNodes = azlinks.DocumentNode.SelectNodes("//div[@class='searchDiv']/table/tr/td/a");
if (azlinkNodes != null)
{
tcs2 = new TaskCompletionSource<bool>();
WebBrowserDocumentCompletedEventHandler documentCompletedHandler2 = (sender2, e2) => tcs2.TrySetResult(true);
if (Uri.TryCreate(baseUri, azlinkNodes[0].Attributes["href"].Value, out newUri))
{
try
{
webBrowser2.Navigate(newUri);
await tcs2.Task;
}
finally
{
webBrowser2.DocumentCompleted -= documentCompletedHandler2;
}
// **************************************************
// will not come out of this test loop
//while (webBrowser2.DocumentText.Length < 10000) {
// webBrowser2.Update();
// System.Threading.Thread.Sleep(500);
//}
MessageBox.Show("webBrowser2.DocumentText.Length = " + webBrowser2.DocumentText.Length.ToString(), "Length");
}
}
}
catch (Exception ex)
{
lastMessage = ex.Message;
}
}
I created a test button on the form so that after the routine had returned to the screen I could click it and check what the second browser was up to.
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("webBrowser2.DocumentText.Length = " + webBrowser2.DocumentText.Length.ToString(), "Length");
}
Upon clicking the button the second browser's .DocumentText length was correct (about 130K+), but I don't see a way to get it to return that in the middle of the routine. As you can see in the commented out code I did a test to see if Update() would help, but it stays forever in the loop.
Does anyone know of a way for it to finish loading without returning to the screen?
Any help would surely be appreciated.
Thanks