geckogeckofxreadystate

Check ReadyState in GeckoFX


I use WebBrowser control for my C# project. I try to integrate GeckoFX instead of WebBrowser. Because i always change inputs in WebBrowser, i have to wait till the website loaded completely. That's why, i use following method:

   private void navigateBrowser(string URL)
        {
            wb.Navigate(URL);
            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                Thread.Sleep(1);
                Application.DoEvents();
            }
        }

i have changed this method for GeckoFX like this: private void navigateBrowser(string URL)

private void navigateBrowser(string URL)
        {
            wb.Navigate(URL);

  while (wb.Document.ReadyState != "complete")
            {
                Application.DoEvents();
            }
            while (wb.IsBusy)
            {
                Application.DoEvents();
            }
}

i call navigateBrowser("http://facebook.com") and then i fill the form for login. After i submit the form, i have to login in another website (in linkedin.com) i call again the navigateBrowser("http://linkedin.com"), when try to call some element from the geckowebbrowser, i get the error "Object reference not set to an instance of an object.". I check the actual URL of the geckowebbrowser, it's still facebook.com, but i should be linkedin.com, because i called linkedin.com.

What can be the problem? How can i solve this issue?


Solution

  • Use the DocumentCompleted event of the webbrowser.

    So, for example - you have a button in your app that will navigate to a specific web page and and a method that will read the page once loaded.

    So, in the DocumentCompleted event hanlder, call that method - this will make sure that the page is already loaded.

    The Application.DoEvents() loop is not a good idea for waiting while the page loads. For example, if something closes/crashes the application during your loop, it will still be running and will not close.

    Regards