.net 限制等待Webbrowser加载的时间

.net 限制等待Webbrowser加载的时间,.net,vb.net,winforms,visual-studio-2012,webbrowser-control,.net,Vb.net,Winforms,Visual Studio 2012,Webbrowser Control,我想限制等待Webbrowser加载的时间; 我从下面的代码开始,该代码在开始下一个操作之前等待Webbrowser加载; 我希望等待时间限制为60秒,如果网页没有加载,那么代码将执行下一个操作; 感谢您的帮助: Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click WebBrowser1.Navigate("www.mekdam.com")

我想限制等待Webbrowser加载的时间; 我从下面的代码开始,该代码在开始下一个操作之前等待Webbrowser加载; 我希望等待时间限制为60秒,如果网页没有加载,那么代码将执行下一个操作; 感谢您的帮助:

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
公共类表单1
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
WebBrowser1.Navigate(“www.mekdam.com”)
而WebBrowser1.ReadyState WebBrowserReadyState.Complete
Application.DoEvents()
结束时
端接头
末级

谢谢

您应该在
DocumentCompleted
事件中检查您的
ReadyState
。这样它就不会挂起你的应用程序

如果添加计时器,则如果加载时间过长,可以取消加载页面:

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
提要
当对象
Timer1
设置为
Enabled
Time1.Start()
时,它调用
Timer1.Tick
函数。因此,将阻止页面在开始时加载。为了防止在时间转义之前加载时出现这种不必要的停止,我们使用变量
secondTime
,以确保第二次调用或给定时间被转义。确认我们使用了
IF
比较,并准备在第二次调用时或在给定间隔后获取中止调用。
计时器1内的条件。勾选
可确保。

这不会添加任何内容,只需在VS 2013中运行您的代码,您就会看到网页在导航时被阻止。所以问题的主要座右铭是等到逃跑的时候,才发生。因此,
secondTime
变量确保了该尝试。OP希望等待页面加载的时间,如果在该时间之后没有加载,则取消页面加载。我将时间设置为2秒,以证明代码按预期工作。像你建议的那样做两次对我来说没有任何意义;我已经接受了你的回答;代码中的注释非常清晰;非常感谢。嗨,马特;我已经发布了一个扩展问题,但没有得到回应;问题是:嗨,马特,我在这里看到了你的答案。它在localhost中运行良好,但当我在WebServer中托管我的站点时,只有在我将域地址与页面一起提供时,它才会工作。若我只给出域地址,那个么它就变成了页面未找到错误
Dim secondTime As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    WebBrowser1.Navigate("http://www.msn.co.uk")
    Timer1.Interval = 100
    Timer1.Enabled = True
    secondTime = False
    Timer1.Start()
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If e.Url.AbsolutePath = CType(sender, WebBrowser).Url.AbsolutePath AndAlso WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        'loading completed before the timer elapsed
        Timer1.Enabled = False
        Timer1.Stop()
    End If
End Sub


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'timer we cancle the loading as it takes long time
    If secondTime = True Then
        WebBrowser1.Stop()  'Second time call
    Else
        secondTime = True   'First time call
    End If
End Sub