限制等待的网页浏览器加载 [英] Limit Waiting for Webbrowser to Load

查看:223
本文介绍了限制等待的网页浏览器加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要限制等待的网页浏览器加载; 我开始与下面的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

这篇关于限制等待的网页浏览器加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆