2012-09-01 25 views
0

我有一個帶有選項卡控件的桌面應用程序(表單),我分配一個選項卡和一個新的自定義webrowser控件。我打開了大約10個這些標籤。每個人訪問大約100 - 500個不同的頁面。如何檢測崩潰tabed webbrowser並處理它?

麻煩的是,如果1個WebBrowser控件有一個問題,它會關閉整個程序。

我希望能夠關閉有問題的WebBrowser控件並打開它的地方一個新的。

是否有我需要訂閱趕上崩潰或反應遲鈍WebBrowser控件任何事件?

我使用C#的窗口上7(表格),.NET框架V4

=========================== ==========================================================

更新:1-選項卡式Web瀏覽器示例

這裏是我有的代碼和我如何以最基本的方式使用webbrowser控件。

  1. 創建一個新形式的項目並命名SimpleWeb
  2. 添加一個新類並將其命名爲myWeb.cs,這裏使用的代碼。
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Security.Policy; 

namespace SimpleWeb 
{ 
    //inhert all of webbrowser 
    class myWeb : WebBrowser 
    { 

     public myWeb() 
     { 
      //no javascript errors 
      this.ScriptErrorsSuppressed = true; 

      //Something we want set? 
      AssignEvents(); 
     } 

     //keep near the top 
     private void AssignEvents() 
     { 

      //assign WebBrowser events to our custom methods 
      Navigated += myWeb_Navigated; 
      DocumentCompleted += myWeb_DocumentCompleted; 
      Navigating += myWeb_Navigating; 
      NewWindow += myWeb_NewWindow; 

     } 


     #region Events 
     //List of events:http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser_events%28v=vs.100%29.aspx 

     //Fired when a new windows opens 
     private void myWeb_NewWindow(object sender, System.ComponentModel.CancelEventArgs e) 
     { 
      //cancel all popup windows 
      e.Cancel = true; 
      //beep to let you know canceled new window 
      Console.Beep(9000, 200); 
     } 


     //Fired before page is navigated (not sure if its before or during?) 
     private void myWeb_Navigating(object sender, System.Windows.Forms.WebBrowserNavigatingEventArgs args) 
     { 

     } 

     //Fired after page is navigated (but not loaded) 
     private void myWeb_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs args) 
     { 

     } 


     //Fired after page is loaded (Catch 22 - Iframes could be considered a page, can fire more than once. Ads are good examples) 
     private void myWeb_DocumentCompleted(System.Object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs args) 
     { 

     } 


     #endregion 



     //Answer supplied by mo. (modified)? 
     public void OpenUrl(string url) 
     { 
      try 
      { 
       //this.OpenUrl(url); 
       this.Navigate(url); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Your App Crashed! Because = " + ex.ToString()); 
       //MyApplication.HandleException(ex); 
      } 
     } 



     //Keep near the bottom 
     private void RemoveEvents() 
     { 
      //Remove Events 
      Navigated -= myWeb_Navigated; 
      DocumentCompleted -= myWeb_DocumentCompleted; 
      Navigating -= myWeb_Navigating; 
      NewWindow -= myWeb_NewWindow; 
     } 
    } 
} 
  1. 在Form1上拖動一個標準的TabControl,並設置碼頭,以填補,你可以進入選項卡的收集和刪除中的預先填充的標籤,如果你喜歡。

  2. 右鍵單擊Form1上,然後選擇「查看代碼」以及與此代碼替換它。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using mshtml; 

namespace SimpleWeb 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 


      //Load Up 10 Tabs 
      for (int i = 0; i <= 10; i++) 
      { 
       newTab("Test_" + i, "http://wwww.yahoo.com"); 

      } 
     } 


     private void newTab(string Title, String Url) 
     { 

      //Create a new Tab 
      TabPage newTab = new TabPage(); 
      newTab.Name = Title; 
      newTab.Text = Title; 

      //create webbrowser Instance 
      myWeb newWeb = new myWeb(); 

      //Add webbrowser to new tab 
      newTab.Controls.Add(newWeb); 
      newWeb.Dock = DockStyle.Fill; 

      //Add New Tab to Tab Pages 
      tabControl1.TabPages.Add(newTab); 

      newWeb.OpenUrl(Url); 


     } 
    } 
} 

保存並運行該項目。

使用以下答案mo。 ,您可以毫無問題地瀏覽第一個網址,但用戶點擊的所有網址又如何呢?我們如何檢查這些?

我不希望將事件添加到頁面上的每個html元素,必須有一種方法可以在導航之前通過函數OpenUrl運行新的URL,而無需進行無限循環。

謝謝。

+3

現代瀏覽器處理這個由一個單獨的進程運行的每個選項卡。這是*正確*解決方案。 –

+0

這就是爲什麼使用鉻爲每個標籤 –

+0

一個單獨的進程@DavidHeffernan我想說的是,在.NET正確的解決辦法是使用不同的'AppDomain's,而不是過程。 –

回答

2

可以使用AppDomain.UnhandledExceptionApplication.ThreadException事件。

但是,以這種方式處理異常可能會導致您的應用程序中出現無效狀態

你能形容,當這些發生異常。
他們是執行方法的結果?

然後,它是更好的處理錯誤的調用方法。

void OpenUrl(Url url) 
{ 
    try 
    { 
     var webBrowser = GetWebBrowser(tabControl.SelectedTab); 
     webBrowser.OpenUrl(url); 
    } 
    catch(SpecificException ex) 
    { 
     MyApplication.HandleException(ex); 
    } 
} 

關於你的評論,試試這個:

try 
{ 
    myCustomWebbrowser.Navigate("yahoo.com"); 
} 
catch(Exception ex) //catch specific exceptions here (catch all is a bad practice) 
{ 
    MessageBox.Alert(ex.Message); //just for testing. 
} 

This article應該幫助你:

private void AssignEvents() 
{ 
    Navigated += myWeb_Navigated; 
    DocumentCompleted += myWeb_DocumentCompleted; 
    Navigating += myWeb_Navigating; 
    NewWindow += myWeb_NewWindow; 
    DownloadComplete += myWen_DownloadComplete; 
} 

void myWen_DownloadComplete(object sender, EventArgs e) 
{ 
    // Check wheter the document is available (it should be) 
    if (Document != null) 
    // Subscribe to the Error event 
    Document.Window.Error += myWeb_Window_Error; 
} 

void myWeb_Window_Error(object sender, HtmlElementErrorEventArgs e) 
{ 
    // We got a script error, record it 
    ScriptErrorManager.Instance.RegisterScriptError(e.Url, 
          e.Description, e.LineNumber); 
    // Let the browser know we handled this error. 
    e.Handled = true; 
} 
+0

我正在嘗試在您的解決方案中工作,我只打電話給myCustomWebbrowser.Navigate(「http://www.yahoo.com」);有一種方法可以告訴webbrowser使用OpenUrl嗎?我把OpenURL放在webbrowser控件中?既然它自己調用,這會不會讓它發現錯誤,不確定這是否是故意的?而不是webBrowser.OpenUrl我使用this.OpenUrl(URL) –

+0

然後簡單地包裝此方法調用。 (在我的回答樣本) –

+0

這將工作的第一個網址,但對其他頁面的網頁瀏覽器craws什麼,有沒有辦法趕上從導航假設web瀏覽器調用導航當一個人手動點擊了網頁瀏覽器的鏈接網址?可以導航被覆蓋一些如何? –