How to detect crashing tabed webbrowser and handle it?

Posted by David Eaton on Stack Overflow See other posts from Stack Overflow or by David Eaton
Published on 2012-09-01T21:15:17Z Indexed on 2012/09/03 9:38 UTC
Read the original article Hit count: 213

Filed under:
|
|
|

I have a desktop application (forms) with a tab control, I assign a tab and a new custom webrowser control. I open up about 10 of these tabs. Each one visits about 100 - 500 different pages.

The trouble is that if 1 webbrowser control has a problem it shuts down the entire program.

I want to be able to close the offending webbrowser control and open a new one in it's place.

Is there any event that I need to subscribe to catch a crashing or unresponsive webbrowser control ?

I am using C# on windows 7 (Forms), .NET framework v4

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

UPDATE: 1 - The Tabbed WebBrowser Example

Here is the code I have and How I use the webbrowser control in the most basic way.

  1. Create a new forms project and name it SimpleWeb
  2. Add a new class and name it myWeb.cs, here is the code to use.
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. On Form1 drag a standard tabControl and set the dock to fill, you can go into the tab collection and delete the pre-populated tabs if you like.

  2. Right Click on Form1 and Select "View Code" and replace it with this code.

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);


        }
    }
}

Save and Run the project.

Using the answer below by mo. , you can surf the first url with no problem, but what about all the urls the user clicks on? How do we check those?

I prefer not to add events to every single html element on a page, there has to be a way to run the new urls thru the function OpenUrl before it navigates without having an endless loop.

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about tabs