Application.DoEvents Takes long time

1.5k Views Asked by At

i have s Sub() like this, to download HTML page using GeckoWebBrowser

wb1 = Nothing
wb1 = New Gecko.GeckoWebBrowser
wb1.Navigate(v_url)
totalticks = 0
loadtimer.Start()
        Do
            If m_stop = True Then Exit Do
            If wb1.IsBusy = False Then 'wb1.ReadyState = WebBrowserReadyState.Complete Then
                If IsNothing(wb1.Document) = False Then
                    If IsNothing(wb1.Document.Body) = False Then
                        Exit Do
                    End If
                End If
            ElseIf wb1.IsBusy = True And String.IsNullOrEmpty(sucessID) = False Then  'wb1.ReadyState = WebBrowserReadyState.Interactive And String.IsNullOrEmpty(sucessID) = False Then
                If IsNothing(wb1.Document) = False Then
                    If IsNothing(wb1.Document.Body) = False Then
                        If InStr(wb1.Document.Body.InnerHtml, sucessID, CompareMethod.Text) <> 0 Then
                            Exit Do
                        End If
                    End If
                End If
            End If
            If totalticks = 15 Then
                'wb1.Dispose()
                wb1.Stop()  'wb1 = New System.Windows.Forms.WebBrowser
                wb1.Reload(Gecko.GeckoLoadFlags.IsRefresh)
                'wb1.ScriptErrorsSuppressed = True
                'wb1.Navigate(v_url)
            ElseIf totalticks >= 30 Then
                wb1.Stop()
                Exit Do
            End If
            'FreeUpMemory()
            Application.DoEvents()
        Loop

My problem is that Application.DoEvents takes a long time to process and finish. P.S i am using STA thread to run this Sub()

2

There are 2 best solutions below

1
On

Ok people here is how my problem was solved, and thanks for the others who tried to help! much appreciated. Ok here it goes:

Basically this 2 lines where in loop

wb1 = Nothing
wb1 = New Gecko.GeckoWebBrowser

So this for some reason was making the messages queue too crowded therfore the application.doevents was taking too long to process those messages. So all i did was decalring wb1 as gloabl object (i.e. dim wb1 as New GeckoWebBrowser) this way i didnt need to set it to nothing then initialize the object again. So anyway i removed the above 2 lines and used the instaniated object instead.

Now things running so smooth and fast, even with DoEvents() !!! YAY!!

1
On

Try using a BackgroundWorker instead of running the Download on the Main thread. You can pass results from the Backgroundworker back to the Main thread through the Result property that gets passed to the RunWorkerCompleted event handler. You don't need the Application.DoEvents in your loop then and still have the Main thread responsive. See this link for a tutorial:

http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx