9

如何为 webBrowser 导航(url)事件设置超时

时间:2019-04-10 标签:c#networking4.0

4

2 回答 2

12

当然是通过使用计时器。例如:

    public void NavigateTo(Uri url) {
        webBrowser1.Navigate(url);
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e) {
        timer1.Enabled = false;
        MessageBox.Show("Timeout on navigation");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        if (e.Url == webBrowser1.Url && timer1.Enabled) {
            timer1.Enabled = false;
            // etc..
        }
    }
于 2010-11-07T19:08:16.160 回答
0

我正在使用基于NavigatingNavigated事件的以下方法。观察这两个事件之间的时间以重定向到主 pgae。

        //Navigation Timer
        timer2.Enabled = true;
        timer2.Interval = 30000;

        br.DocumentCompleted += browser_DocumentCompleted;
        br.DocumentCompleted += writeToTextBoxEvent;
        br.Navigating += OnNavigating;
        br.Navigated  += OnNavigated;

        br.ScriptErrorsSuppressed = true;
        br.Navigate(ConfigValues.websiteUrl);

    private void OnNavigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        //Reset Timer
        timer2.Stop();
        timer2.Start();

        WriteLogFunction("OnNavigating||||||"+e.Url.ToString());
    }

    private void OnNavigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        //Stop Timer
        timer2.Stop();

        WriteLogFunction("NAVIGATED <><><><><><><> " + e.Url.ToString());
    }


    private void timer2_Tick(object sender, EventArgs e)
    {
        WriteLogFunction(" Navigation Timeout TICK");
        br.Stop();
        br.Navigate(ConfigValues.websiteUrl);
    }

参考

  1. 为 webbrowser 加载方法创建超时
  2. 如果页面无法加载,浏览器超时
于 2014-04-04T17:30:39.650 回答