Link to home
Start Free TrialLog in
Avatar of MiStACoRn
MiStACoRn

asked on

Web Browser Control - Waiting for a Page to Load

I'm developing an application and I'm trying to keep it as object oriented as possible.  The application needs to collect information from web pages here and there and/or fill out forms on a website.

The problem I run into is this:  Traditionally.. most people will tell you that the best way to wait for a website to finish loading is this:

Do:
    Application.Doevents
Loop Until WB.IsBusy = False

This is all well and good and works, but firing doevents that many times causes a high cpu usage, and this does not seem practical.  Another alternative I have heard mentioned is to call System.Threading.Thread.Sleep(10).  This does lower the CPU usage, but it brings the entire thread to a complete stop, which is also not a practical solution.

WB.NewURL("www.google.com")
'Wait to recieve document complete event back from browser class
Do more stuff.

This SHOULD be able to happen, right?  It would be ridiculous to set up variables to keep track of what's currently happening and what to do next instead of just waiting until the browser is done loading with an event.  I'm just not sure how to make the particular function I'm in not process the next line of code until it recieves the event from my browser class without looping and using Application.Doevents.  Or maybe there's a better way to implement this?
Avatar of PaulHews
PaulHews
Flag of Canada image

Simple example... Use the WebBrowser DocumentCompleted event.  (Note the test within to make sure it doesn't fire multiple times for frames.)
Public Class Form1
 
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        WebBrowser1.Navigate("http://www.google.com")
    End Sub
 
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
 
        If e.Url = WebBrowser1.Url Then
            MsgBox("Document Completed")
        End If
 
    End Sub
End Class

Open in new window

Avatar of MiStACoRn
MiStACoRn

ASKER

This isn't what I mean.  I want to be able to call a new page from the middle of another function, and then have that function wait before it continues processing until the documentcomplete event fires.  If there a way to hold up on processing that next line without looping?  I just think it's really messy to write a billion functions based off events and keep track of what particular procedure is going on to handle what to do next.  Some of the procedures I'm going to build are going to use several websites in and of themselves.
>it's really messy to write a billion functions based off events and keep track of what particular procedure is going on to handle what to do next.

I agree that it's messy, but the WebBrowser control uses an asynchronous model, and it has been my experience that it's better to go with the model exposed by the object.  The asynchronous model greatly reduces the cpu activity of your application.  However, it's easy enough to implement a loop that does what you want.

Do
    System.Threading.Thread.Sleep(10)
    Application.DoEvents()
Loop Until WB.IsBusy = False

Although your thread will be sleeping, it's for very small periods and prevents your application from hogging the CPU time.  Because of the DoEvents, it will still appear to be responsive to user actions.  You can play with the millisecond value: I've used up to 50 without any ill effect.
Yeah if you look at my original post, these are things that I've already been implementing.  I'm just working on the newer version and I'd like to make it as event driven as possible.. but the amount of event handlers I'd have to create just seem absolutely ridiculous when compared to the loop.  I thought there might be a way to pause the function until an event triggers without looping.
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin