How to set timeout for webBrowser navigate event

前端 未结 2 915
野的像风
野的像风 2021-01-02 07:45

how can i set timeout for webBrowser navigate (url) event

c# netframework 4.0

2条回答
  •  误落风尘
    2021-01-02 08:27

    I am using following approach based on Navigating and Navigated events. The time between these two events are observed for redirecting to home 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);
        }
    

    Reference

    1. Create a time-out for webbrowser loading method
    2. webbrowser timeout if page wont load

提交回复
热议问题