2010-11-07 89 views

回答

11

使用過程中的定時器超時。例如:

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

那不是等待web瀏覽器來抵消,而不是結束它一定量的前後實際完成導航時間? – 2010-11-07 19:13:23

+2

呃,沒有。沒有什麼可以結束的。只需導航到別處。 – 2010-11-07 19:16:08

+1

所以當計時器打勾時,我會在其他地方導航? – MonsterMMORPG 2010-11-07 19:43:55

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. Create a time-out for webbrowser loading method
  2. webbrowser timeout if page wont load
+0

那麼br.Stop()會導致Web瀏覽器控件停止嘗試導航? – crush 2017-12-14 03:03:05