0

I am currently working with a webBrowser control in a WinForm -

public Form1()
        {
            InitializeComponent();

            webBrowser1.AllowNavigation = true;
            webBrowser1.Navigate("http://foo.bar");        
        }

I have a button control that takes the webBrowser1.Url.OriginalString and sets it to a textBox -

// On button_Click
string requestResponse = webBrowser1.Url.OriginalString;
requestURLtextBox.Text = requestResponse;

However if the button is clicked before webBrowser1 has had enough time to get the OriginalString text, this will error as the value does not exist yet.

I tried adding -

while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
      Thread.Sleep(1000);  
}

Before the previous code in the button_Click event however this went into an infinite loop. How can I retrieve the OriginalString after the webBrowser is complete?

Ebikeneser
  • 2,582
  • 13
  • 57
  • 111
  • 2
    [Similar Question](http://stackoverflow.com/questions/583897/c-sharp-how-to-wait-for-a-webpage-to-finish-loading-before-continuing) : looks like you want the DocumentCompleted event – Jonesopolis Oct 22 '13 at 15:28
  • +1 for `DocumentCompleted`. Using `async/await` can make it [easier](http://stackoverflow.com/questions/18572635/webbrowser-behaviour-issues/18573522#18573522) to use, VB.NET has this feature too. – noseratio Oct 22 '13 at 20:31

1 Answers1

1

Here some VB simple code that may work for you:

    If mbBusy Then Exit Function ' form level variable
    mbBusy = True
    Web.Navigate("http://.....htm")
    dtWebWait = Now().AddSeconds(timeoutSeconds)
    Do Until Web.ReadyState = WebBrowserReadyState.Complete
        Application.DoEvents()
        If Now() > dtWebWait Then
            MsgBox("navigate timeout - search form")
            Exit Function
        End If
        Threading.Thread.Sleep(250)
    Loop
    mbBusy = False
rheitzman
  • 2,247
  • 3
  • 20
  • 36
  • 1
    This is [busy waiting](http://stackoverflow.com/a/19019200/1768303), at least one core of your CPU will be cruising at 100%. This pattern should be avoided. – noseratio Oct 23 '13 at 01:16
  • I agree this is not a good technical solution but can get the job done for simple tasks. Question - if there was a wait/sleep in the inner loop would that wait/sleep block the web browser? – rheitzman Oct 23 '13 at 16:13
  • A short sleep like 100-200ms inside the loop would somewhat mitigate busy waiting and would not block the browser, as long as `Application.DoEvents` is still called. But the potential problems of the nested message loop, like code re-entrancy, will remain until the loop is over. – noseratio Oct 23 '13 at 21:30
  • 1
    @Noseratio Would the added (edits in code above) Busy flag and Sleep tighten things up? – rheitzman Oct 23 '13 at 22:13
  • Yes it certainly improves things. If you like going a bit further, check the `Wait` implementation from [here](http://support.microsoft.com/kb/231298), which uses a waitable timer and `MsgWaitForMultipleObjects`. Using such `Wait` instead of `Thread.Sleep` would be much more efficient, because it will return as soon as there's a message in the queue for `DoEvents` to pick up. This wouldn't however eliminate the [potential issues with DoEvents](http://stackoverflow.com/a/5183623/1768303), as asynchronous handling of `DocumentCompleted` would. Anyway, +1 for your efforts. – noseratio Oct 24 '13 at 00:10