0

I'm running a backgroundworker for my webbrowser on a winform, it works and all, but why does the UI lags or freezes for a moment to wait for the page to load when I want it to load in the background so that the UI is fully functional?

Private Property pageready As Boolean = False

Public Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Public Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Try
        WebBrowser1.Navigate("http://www.mypage.com")
        WaitForPageLoad() >>>> Lags and freezes UI for a sec or two to wait for page loading. How can i do this in the background?
    Catch ex As Exception
    End Try
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    'Here i will get my attributes off the webpage ect....
End Sub
  • Your DoWork method calls a method which is a do-nothing loop. You dont need both a BGW and a homemade wait loop. Why cant you just respond to the `DocumentCompleted` event? – Ňɏssa Pøngjǣrdenlarp Sep 15 '16 at 16:17
  • Well, i need the page to load first before i can do any getting attributes from the documentcompleted event. – JustAnotherPersonYouDontKnow Sep 15 '16 at 16:22
  • Never use `Application.DoEvents()` to keep your application responsive, _**and especially not from another thread!**_ See [**this answer**](http://stackoverflow.com/a/5183623/3740093) and [**this blog post**](https://blogs.msdn.microsoft.com/jfoscoding/2005/08/06/keeping-your-ui-responsive-and-the-dangers-of-application-doevents/) for info about why using `Application.DoEvents()` is bad unless you know what you are doing. – Visual Vincent Sep 15 '16 at 16:32
  • @VisualVincent thanks but please focus on the main question. Thanks – JustAnotherPersonYouDontKnow Sep 15 '16 at 18:33
  • I am focusing on the main question. You are calling `Application.DoEvents()` from a background thread with no delay. Every time it's called it will deliver window messages (such as paint messages) to the UI, resulting in stacking as many messages as possible as often as possible, which will make the UI process messages even more often than what it already does. This will also likely drive the CPU up to 100%. – Visual Vincent Sep 15 '16 at 20:04
  • Replace `Application.DoEvents()` with a `ManualResetEvent` to pause the thread until the page has loaded. See one of my previous questions and its accepted answer: http://stackoverflow.com/questions/38546101/would-looping-thread-sleep-be-bad-for-performance-when-used-to-pause-a-thread – Visual Vincent Sep 15 '16 at 20:06
  • @VisualVincent is there anyway to handle a popup dialog when the webbrowser finishes loading? I can't seem to close it even though i have a method in the webbrowser.documentcompleted because the dialog shows before it gets to that point. – JustAnotherPersonYouDontKnow Sep 15 '16 at 20:25
  • What kind of popup? A normal message box? A new window? If it's a message box the option I can think of is sending an `ENTER` key press to the window by searching for it by its title (see the [**FindWindow**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx) WinAPI function). – Visual Vincent Sep 15 '16 at 21:27

0 Answers0