【问题标题】:How to set timeout for webBrowser navigate event如何为 webBrowser 导航事件设置超时
【发布时间】:2011-05-06 08:55:29
【问题描述】:

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

c#网络框架4.0

【问题讨论】:

    标签: c# timeout browser set navigatetourl


    【解决方案1】:

    当然是使用定时器。例如:

        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..
            }
        }
    

    【讨论】:

    • 这不是要等浏览器真正完成导航再取消,而不是在一定时间后结束导航吗?
    • 呃,没有。没有什么可以结束的。只需导航到其他地方。
    • 所以当计时器计时我会导航到其他地方?
    • 我想结束之前的导航事件。如果不导航到其他地方,我怎么能做到这一点?
    • webBrowser1.DocumentText = "取消";会做的。
    【解决方案2】:

    我正在使用基于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. Create a time-out for webbrowser loading method
    2. webbrowser timeout if page wont load

    【讨论】:

    • br.Stop() 是否会导致 Web 浏览器控件停止尝试导航?
    猜你喜欢
    • 2019-02-09
    • 2018-02-04
    • 1970-01-01
    • 2018-09-04
    • 2012-12-16
    • 1970-01-01
    • 2011-11-10
    • 2020-05-16
    相关资源
    最近更新 更多