0

I want to know if there are anyway to run the actions of a webbrowser behind the scene without freezing the main UI? I want to be doing other stuff on the main UI and have the webbrowser do stuff behind the scene from an button event. Any way to do this? I've looked into background worker but don't seem to be working with webbrowser coms ect. Here's what I've tried but keeps freezing/lagging on wait for webpage to load:

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
  • `I want to know if there are anyway to run the actions of a webbrowser behind the scene without freezing the main UI?` ***most certainly yes***, what have you tried? – Trevor Sep 15 '16 at 19:05
  • @Zaggler check updated post.It does what it should, but it pauses to wait for the page to load, that is when i want it to do all in background and not on the UI. If i don't use pause, it will not wait and therefore anything after that will not get runned. – JustAnotherPersonYouDontKnow Sep 15 '16 at 19:07
  • Why are you using a custom Doc completed event when the webbrowser already has one? Not sure why you're using a background worker. You should just be able to call the navigate method. Behind the scenes, the page is loading in the webbrowser, meanwhile the UI is free to do whatever. When the page loads, it fires the doc completed event and then you get your attributes. – Chase Rocker Sep 15 '16 at 19:24
  • @ChaseRocker when i click the button, if i use navigate, my UI freezes until the webbrowser navigates to the page, I don't want that to freeze, i want it to navigate in the background ect. – JustAnotherPersonYouDontKnow Sep 15 '16 at 19:33
  • `Application.DoEvents()` remove this... why are you pumping window messages? Also you seem to be confused about background workers, why have one for what you want to do.... – Trevor Sep 15 '16 at 19:45
  • 1
    How is this significantly different than [your last question](http://stackoverflow.com/q/39515807/1070452)? – Ňɏssa Pøngjǣrdenlarp Sep 15 '16 at 19:46
  • 1
    @Plutonix you beat me to it ... – Trevor Sep 15 '16 at 19:48
  • Asking the same question again will most likely not help you get more answers, only making it get marked as a duplicate. I have explained that `DoEvents()` is a bad call, which I'm very sure is what's causing your issues. I linked you to an alternate solution in your original question. – Visual Vincent Sep 15 '16 at 20:17

0 Answers0