为什么在等待页面加载到webbrowser上时,backgroundworker会滞后或冻结一点?

时间:2016-09-15 16:13:06

标签: vb.net winforms webbrowser-control backgroundworker

我在winform上为我的webbrowser运行了一个后台工作程序,它可以正常工作,但是当我希望它在后台加载时,为什么UI会滞后或冻结等待页面加载用户界面完全正常运作?

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

0 个答案:

没有答案