限制等待的网页浏览器加载加载、网页浏览器

2023-09-06 11:11:07 作者:始于心动,终于白首

我要限制等待的网页浏览器加载; 我开始与下面的code正在等待的网页浏览器开始下一个动作之前加载; 我想等待被限定于60秒时,如果该网页不加载则code将执行下一个动作; 任何帮助是AP preciated:

I want to Limit Waiting for Webbrowser to Load; I started with the code below that is waiting for Webbrowser to load before starting next action; I want the waiting to be limited to 60 seconds, if the webpage does not load then the code will perform the next action; Any help is appreciated:

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    WebBrowser1.Navigate("www.mekdam.com")
    While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    End While
End Sub
End Class

感谢

推荐答案

您应该检查你的的readyState DocumentCompleted 事件。这样,就不会挂起你的应用程序。

You should check your ReadyState in the DocumentCompleted event. That way it won't hang your application.

如果您添加一个计时器可以取消页面的加载,如果时间太长:

If you add a timer you can cancel the loading of the page if it takes too long:

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    WebBrowser1.Navigate("http://www.msn.co.uk")
    Timer1.Interval = 2000
    Timer1.Enabled = True
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    'If the timer is not running then we are not waiting for the document to complete
    If Not Timer1.Enabled Then Exit Sub

    'Check the document that just loaded is the main document page (not a frame) and that the control is in the ready state
    If e.Url.AbsolutePath = CType(sender, WebBrowser).Url.AbsolutePath AndAlso WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        'we loaded the page before the timer elapsed
        Timer1.Enabled = False
    End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'timer has elapsed so cancel page load
    WebBrowser1.Stop()
    'Disable the timer
    Timer1.Enabled = False
End Sub