I need to obtain some information from a webpage and display the same on the MainWindow. I am calling a routine written in a separate library whose properties provide these information to,once the webPage is loaded.
I have looked around a lot on stackoverflow and found various solutions;but none worked for me.Here is my routine:
protected System.Windows.Forms.WebBrowser wb;
public void ObtainProfileInformation()
{
url = CUSTOMER_PROFILE_URL;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(url));
try
{
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
var th = new System.Threading.Thread(() =>
{
wb = new System.Windows.Forms.WebBrowser() { Visible = false, ScriptErrorsSuppressed = true };
wb.DocumentCompleted += (sender, e) =>
{
string target_url = wb.Url.AbsoluteUri;
if ((target_url.Contains("login") == false))
{
var html_text = wb.DocumentText;
HtmlAgilityPack.HtmlDocument html_document = new HtmlAgilityPack.HtmlDocument();
html_document.LoadHtml(html_text);
foreach (var MatchingDiv in html_document.DocumentNode.Descendants("span"))
{
if (MatchingDiv.Id.Contains("lblFullName"))
{
CustomerName = MatchingDiv.InnerText;
}
if (MatchingDiv.Id.Contains("lblCompany"))
{
CompanyName = MatchingDiv.InnerText;
}
if (MatchingDiv.Id.Contains("lblTitle"))
{
CustomerTitle = MatchingDiv.InnerText;
}
if (MatchingDiv.Id.Contains("lblEmail"))
{
CustomerEmail = MatchingDiv.InnerText.Split(new char[] { ':' })[1].TrimStart();
}
if (MatchingDiv.Id.Contains("lblUsername"))
{
CustomerUserName = MatchingDiv.InnerText.Split(new char[] { ':' })[1].TrimStart();
}
}
}
System.Windows.Forms.Application.ExitThread();
};
wb.Navigate(url);
System.Windows.Forms.Application.Run();
});
th.SetApartmentState(System.Threading.ApartmentState.STA);
th.Start();
}
catch (WebException)
{
wb.Dispose();
}
catch (Exception ex)
{
wb.Dispose();
throw new Exception("Something strange", ex);
}
}
DocumentCompleted event is getting fired and the properties CustomerName etc. are getting filled but after the load event of the Window is completed.So i am unable to change them on the Window.