有没有办法在后台运行表单上的webbrowser?

时间:2016-09-15 19:01:39

标签: vb.net

我想知道是否有任何方式在场景后面运行webbrowser的操作而不冻结主UI?我想在主UI上做其他的事情,让webbrowser从按钮事件开始做幕后工作。有什么办法吗?我已经调查了后台工作者,但似乎没有使用webbrowser coms等。这是我尝试过但在等待网页加载时一直冻结/滞后:

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 个答案:

没有答案