Home All Groups Group Topic Archive Search About

Webbrowser UserControl

Author
4 Oct 2005 4:56 PM
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

Author
5 Oct 2005 8:02 AM
Jakob Christensen
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.
Show quote
"xela" wrote:

> 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
>
>
>
Author
5 Oct 2005 4:35 PM
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


Show quote
"Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
news:025DE366-8571-4A2B-A28C-B4AC3ACFA30C@microsoft.com...
> 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.
> --
> http://www.dotninjas.dk
> http://www.powerbytes.dk
>
>
> "xela" wrote:
>
> > 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
> >
> >
> >
Author
6 Oct 2005 6:06 AM
Jakob Christensen
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.
Show quote
"xela" wrote:

> 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
>
>
> "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> news:025DE366-8571-4A2B-A28C-B4AC3ACFA30C@microsoft.com...
> > 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.
> > --
> > http://www.dotninjas.dk
> > http://www.powerbytes.dk
> >
> >
> > "xela" wrote:
> >
> > > 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
> > >
> > >
> > >
>
>
>
Author
8 Oct 2005 4:38 AM
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


Show quote
"Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
news:BC2D4FE4-7875-4FBB-849A-805590D39919@microsoft.com...
> 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.
> --
> http://www.dotninjas.dk
> http://www.powerbytes.dk
>
>
> "xela" wrote:
>
> > 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
> >
> >
> > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> > news:025DE366-8571-4A2B-A28C-B4AC3ACFA30C@microsoft.com...
> > > 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.
> > > --
> > > http://www.dotninjas.dk
> > > http://www.powerbytes.dk
> > >
> > >
> > > "xela" wrote:
> > >
> > > > 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
> > > >
> > > >
> > > >
> >
> >
> >
Author
8 Oct 2005 12:10 PM
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


Show quote
"xela" <xelavb***@yahoo.com.hk> wrote in message
news:OFxhkF8yFHA.3312@TK2MSFTNGP09.phx.gbl...
> 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
>
>
> "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> news:BC2D4FE4-7875-4FBB-849A-805590D39919@microsoft.com...
>> 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.
>> --
>> http://www.dotninjas.dk
>> http://www.powerbytes.dk
>>
>>
>> "xela" wrote:
>>
>> > 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
>> >
>> >
>> > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
>> > news:025DE366-8571-4A2B-A28C-B4AC3ACFA30C@microsoft.com...
>> > > 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.
>> > > --
>> > > http://www.dotninjas.dk
>> > > http://www.powerbytes.dk
>> > >
>> > >
>> > > "xela" wrote:
>> > >
>> > > > 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
>> > > >
>> > > >
>> > > >
>> >
>> >
>> >
>
>
Author
11 Oct 2005 5:19 AM
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

Show quote
"Charles Law" <bl***@nowhere.com> wrote in message
news:uM1hXFAzFHA.1256@TK2MSFTNGP09.phx.gbl...
> 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
>
>
> "xela" <xelavb***@yahoo.com.hk> wrote in message
> news:OFxhkF8yFHA.3312@TK2MSFTNGP09.phx.gbl...
> > 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
> >
> >
> > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> > news:BC2D4FE4-7875-4FBB-849A-805590D39919@microsoft.com...
> >> 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.
> >> --
> >> http://www.dotninjas.dk
> >> http://www.powerbytes.dk
> >>
> >>
> >> "xela" wrote:
> >>
> >> > 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
> >> >
> >> >
> >> > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> >> > news:025DE366-8571-4A2B-A28C-B4AC3ACFA30C@microsoft.com...
> >> > > 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.
> >> > > --
> >> > > http://www.dotninjas.dk
> >> > > http://www.powerbytes.dk
> >> > >
> >> > >
> >> > > "xela" wrote:
> >> > >
> >> > > > 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
> >> > > >
> >> > > >
> >> > > >
> >> >
> >> >
> >> >
> >
> >
>
>
Author
11 Oct 2005 6:14 PM
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


Show quote
"xela" <xelavb***@yahoo.com.hk> wrote in message
news:eHtgqKizFHA.3812@TK2MSFTNGP09.phx.gbl...
> 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
>
> "Charles Law" <bl***@nowhere.com> wrote in message
> news:uM1hXFAzFHA.1256@TK2MSFTNGP09.phx.gbl...
>> 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
>>
>>
>> "xela" <xelavb***@yahoo.com.hk> wrote in message
>> news:OFxhkF8yFHA.3312@TK2MSFTNGP09.phx.gbl...
>> > 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
>> >
>> >
>> > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
>> > news:BC2D4FE4-7875-4FBB-849A-805590D39919@microsoft.com...
>> >> 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.
>> >> --
>> >> http://www.dotninjas.dk
>> >> http://www.powerbytes.dk
>> >>
>> >>
>> >> "xela" wrote:
>> >>
>> >> > 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
>> >> >
>> >> >
>> >> > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
>> >> > news:025DE366-8571-4A2B-A28C-B4AC3ACFA30C@microsoft.com...
>> >> > > 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.
>> >> > > --
>> >> > > http://www.dotninjas.dk
>> >> > > http://www.powerbytes.dk
>> >> > >
>> >> > >
>> >> > > "xela" wrote:
>> >> > >
>> >> > > > 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
>> >> > > >
>> >> > > >
>> >> > > >
>> >> >
>> >> >
>> >> >
>> >
>> >
>>
>>
>
>
Author
10 Oct 2005 12:20 PM
Jakob Christensen
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.

Show quote
"xela" wrote:

> 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
>
>
> "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> news:BC2D4FE4-7875-4FBB-849A-805590D39919@microsoft.com...
> > 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.
> > --
> > http://www.dotninjas.dk
> > http://www.powerbytes.dk
> >
> >
> > "xela" wrote:
> >
> > > 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
> > >
> > >
> > > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> > > news:025DE366-8571-4A2B-A28C-B4AC3ACFA30C@microsoft.com...
> > > > 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.
> > > > --
> > > > http://www.dotninjas.dk
> > > > http://www.powerbytes.dk
> > > >
> > > >
> > > > "xela" wrote:
> > > >
> > > > > 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
> > > > >
> > > > >
> > > > >
> > >
> > >
> > >
>
>
>
Author
11 Oct 2005 5:16 AM
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

Show quote
"Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
news:1EB95CE9-9B0E-4A37-BA59-BA5B96325B68@microsoft.com...
> 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.
>
> --
> http://www.dotninjas.dk
> http://www.powerbytes.dk
>
>
> "xela" wrote:
>
> > 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
> >
> >
> > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> > news:BC2D4FE4-7875-4FBB-849A-805590D39919@microsoft.com...
> > > 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.
> > > --
> > > http://www.dotninjas.dk
> > > http://www.powerbytes.dk
> > >
> > >
> > > "xela" wrote:
> > >
> > > > 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
> > > >
> > > >
> > > > "Jakob Christensen" <j**@REMOVEpension.dk> wrote in message
> > > > news:025DE366-8571-4A2B-A28C-B4AC3ACFA30C@microsoft.com...
> > > > > 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.
> > > > > --
> > > > > http://www.dotninjas.dk
> > > > > http://www.powerbytes.dk
> > > > >
> > > > >
> > > > > "xela" wrote:
> > > > >
> > > > > > 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
> > > > > >
> > > > > >
> > > > > >
> > > >
> > > >
> > > >
> >
> >
> >

AddThis Social Bookmark Button