C# 如何设置webBrowser导航事件的超时

C# 如何设置webBrowser导航事件的超时,c#,timeout,browser,set,navigatetourl,C#,Timeout,Browser,Set,Navigatetourl,如何设置webBrowser导航(url)事件的超时 当然,c#netframework 4.0是通过使用计时器实现的。例如: public void NavigateTo(Uri url) { webBrowser1.Navigate(url); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { timer1.E

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


当然,c#netframework 4.0是通过使用计时器实现的。例如:

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

我基于
导航
导航
事件使用以下方法。观察这两个事件之间的时间,以便重定向到主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);
    }
//导航计时器
timer2.Enabled=true;
时间间隔=30000;
br.DocumentCompleted+=浏览器\文档已完成;
br.DocumentCompleted+=WriteToTextBoxesEvent;
br.导航+=正在导航;
br.已导航+=已导航;
br.ScriptErrorsSuppressed=true;
br.导航(ConfigValues.websiteUrl);
导航时的私有无效(对象发送方,WebBrowserNavigatingEventArgs e)
{
//重置计时器
timer2.Stop();
timer2.Start();
WriteLogFunction(“OnNavigating”+e.Url.ToString());
}
已导航的专用void(对象发送方,WebBrowserNavigatedEventArgs e)
{
//停止计时器
timer2.Stop();
WriteLogFunction(“导航的”+e.Url.ToString());
}
私有无效计时器2_刻度(对象发送方,事件参数e)
{
WriteLogFunction(“导航超时勾选”);
br.停止();
br.导航(ConfigValues.websiteUrl);
}
参考


  • 这难道不是要等到WebBrowser在取消之前真正完成导航,而不是在一段时间后结束它吗?呃,不,没有什么可以结束的。只需导航到其他地方。所以当计时器勾选时,我将导航到其他地方?我想结束上一个导航事件。如何在不导航到其他地方的情况下执行此操作?webBrowser1.DocumentText=“已取消”;也可以。
    br.Stop()
    是否会导致web浏览器控件停止尝试导航?