Webbrowser UserControl

X

xela

I'm developing a UserControl that essentially a wrapper for Webbrowser.
The caller module will call the Navigate() method for document download.
It then call WaitOne() on a ManualResetEvent object. The ManualResetEvent
object is supposed
to be Set() from WebBrowser's DocumentComplete event handler when download
completed.

However, when Navigate() is called, it simply stops at the WaitOne() call
and never get the Set() from DocumentCompleted event.

Can anyone give a hint ?

The reason why I try to use WaitOne() is that the Application.Doevents is
consuming a lot of CPU resource unnecessarily.

Thanks
 
G

Guest

In your case the event handler for DocumentComplete is called on the same
thread as the one calling WaitOne. This means that your call to WaitOne
freezes your application and it is unable to respond to the event.

HTH, Jakob.
 
X

xela

Dear Jakob,

I have tried to use a separate thread to navigate. However, it seems the
thread has quickly returned, left only the main thread being blocked by
WaitOne().

Please find below my coding.
I was plagued by this for a long time. Please help.
Thanks


Public Class CtlBrowser
Private mManualEvent1 As ManualResetEvent
Private msURL As String

Public Sub GoToURL(ByVal sURL As String)
Dim t As Thread
Dim i As Integer

Try
mManualEvent1 = New ManualResetEvent(False)

msURL = sURL
t = New Thread(AddressOf LoadPage)
t.Start()

mManualEvent1.WaitOne()
Catch
Console.WriteLine("ERROR: GoToURL()")
End Try
End Sub



Private Sub LoadPage()
IE.Navigate(msURL)
End Sub


Private Sub IE_DocumentComplete(ByVal sender As Object, ByVal e As
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles
IE.DocumentComplete
Dim sURL As String

Try
If (msURL).Equals(e.uRL) Then
mManualEvent1.Set()
End If
Catch
Console.WriteLine("ERROR")
End Try
End Sub

End Class
 
G

Guest

Accessing controls on a form from another thread is not allowed so spawning
another thread to do the work might get you into trouble.

Why is it that you want the main thread to stop and wait while the browser
loads the page? You wrote that DoEvents is taking a lot of processing but
why do you need to call DoEvents if the alternative is to halt the main
thread altogether?

Please explain what you are trying to accomplish and I will try to find a
solution :)

Regards, Jakob.
 
X

xela

Dear Jakob,

I use a Form to hold a UserControl that in turn holds a Webbrowser. The
purpose is just to modularize the function so that it can be reused.

Below is the caller coding on the Form that holding the UserControl. I use
Application.Doevents to wait for the DocumentComplete event to fire and
raise another event to the Form that sets mnStatus to 0. Web pages sometimes
take half minute or more to load and therefore the Application.Doevents is
in a loop and this will consume a lot of CPU resource. If time elapsed is
longer than nWait (in seconds), then ShowPage will return false that signals
a failure.

To improve on the performance, I explore another solution, ie. to use
WaitOne(). If it works, then I think Application.Doevents will no longer be
needed.

Hopefully, you can understand what I mean.

Thanks


Public Function ShowPage(ByVal sURL As String, ByVal nWait As Integer,
ByVal nRetry As Integer) As Boolean
Dim i As Integer
Dim j As Integer

ShowPage = True

Try
For i = 0 To nRetry
Timer1.Enabled = True
mnStatus = 1
mnSecond = 0

brw.GoToURL(sURL)
Do While (mnStatus = 1)
Application.DoEvents()

If mnStatus = 0 Then
Timer1.Enabled = False
Exit Function
End If
If mnSecond >= nWait Then
Exit Do
End If
Loop
Next

Timer1.Enabled = False
ShowPage = False
Catch ex As Exception
Timer1.Enabled = False
ShowPage = False
Console.WriteLine(ex.StackTrace)
End Try
End Function
 
C

Charles Law

xela

Why not retain the loop with DoEvents, but each time around the loop go to
sleep for, say, 100 ms? This way your cpu percentage will drop to almost
nothing and the application will still be responsive.

HTH

Charles
 
G

Guest

You write that you want ShowPage to return false, if the web page takes
longer than a given number of seconds to complete. Do you want ShowPage to
cancel the page request if it takes to long and then return false or what is
the idea?

Regards, Jakob.
 
X

xela

Dear Jakob,

If the web page just takes too long to download (longer than the given no.
of seconds), the ShowPage function just return false to the calling routine
to indicate the failure; otherwise return true.

Regards
 
X

xela

Dear Charles,

Since the download thread actually is the main thread, unless you can
separate the threads, if you issue sleep command, the downloading action
will also be stopped.

Regards
 
C

Charles Law

xela
Since the download thread actually is the main thread, unless you can
separate the threads, if you issue sleep command, the downloading action
will also be stopped.

That's the reason for keeping the period short. Using 100 ms will still
allow your page to download and will keep the cpu percentage low as well.
Have you tried it?

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top