开发者

How to set timeout for webBrowser navigate event

开发者 https://www.devze.com 2023-01-23 18:00 出处:网络
how can i set timeout for webBrowser navigate (url) event c# netfra开发者_运维知识库mework 4.0By using a Timer of course.For example:

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

c# netfra开发者_运维知识库mework 4.0


By using a Timer of course. For example:

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消