tags:

views:

440

answers:

4

I've been using Google Chrome for a while now and I noticed that it features very elegant crash control.

Just before crashing, google chrome gave a message saying "Woah! Google Chrome has crashed. Restart now?". And right after, I'd get a standard Windows XP "This program has encountered a problem and needs to close." with the "Debug", "Don't send" and "Send Error Report" buttons.

My question is how can you program your compiled application to detect a crash condition in advance? If you have knowledge of how to do it in any programming language / platform would be great.

Thanks

+16  A: 

Google Chrome uses a technique (often called process separation) where the 'host' UI manages child processes that it can detect becoming unresponsive (or worse, throwing an error and closing). It starts a new process for each tab you open.

Here's an article describing this in a bit more detail.

Using .net's Process class, you can start processes, check if they're responsive and even kill them.

If you want to embed a process' window within your own, you can use platform functions such as SetParent to move one window within another. I'm afraid I'm not aware of a managed alternative to SetParent when working with Forms, but I suspect one exists so it's worth searching for that before using SetParent.

If the host process crashes, simply using something like AppDomain.CurrentDomain.UnhandledException will allow you to receive a notification that a top-level exception has occurred, but by this stage you're unlikely to be able to predict the state of objects within your app and restarting (along with some logging and a notification to the user) is probably your only sensible option.

Handling top-level exceptions is covered in detail here.

Matthew Brindley
+4  A: 

In .NET, you can hook up to the System.AppDomain.CurrentDomain.UnhandledException event. Your code would look something like this:

using System;
using System.Windows.Forms;

public class Program
{
    public static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += MyUnhandledExceptionHandler;

        // start rest of application
    }

    private static void MyUnhandledExceptionHandler(object sender, EventArgs args)
    {
        MessageBox.Show("Your app is crashing.  Watch out!");
    }
}
skb
Nice - I always like seeing some code samples when someone answers a question.
Ogre Psalm33
You need to do more than that, see http://www.danielmoth.com/Blog/2004/08/global-exception-handling-net-v11-part.html
mhenry1384
+12  A: 

I don't know Google's code so I am speculating. Google Chrome probably isn't predicting that it will crash, but detecting that it has crashed.

In Windows you can do this by providing a handler for all unhandled exceptions. In this handler you might do things such as restart the application, create a minidump file, etc.

Take a look at SetUnhandledExceptionFilter Function for one method.

John Dibling
Note that the reason the Windows debug window comes up afterwards is that google goes ahead and throws the error to the OS after it is done with it.
Chris Lively
Sort of. It's detecting that one of its child processes crashed.
jeffamaphone
+3  A: 

There is a restart API. Daniel Moth has blogged about it here and here. Please note I am not saying this is the way Google Chrome works, just that it is something you might want to look into.

I believe you could also load bits of the app in seperate App Domains. I believe this is what the .NET 3.5 Addin framework uses, though I can't say I've ever used it (merely read about it). Looks like Daniel has blogged about this too.

RichardOD