7

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

1
  • 6
    Upon first glance at this question, I wondered if this was going to be about whether Google had resolved the Halting Problem...
    – J. Polfer
    Jun 12, 2009 at 20:51

4 Answers 4

16

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.

12

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.

2
  • 3
    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.
    – NotMe
    Jun 12, 2009 at 20:47
  • Sort of. It's detecting that one of its child processes crashed.
    – i_am_jorf
    Jun 12, 2009 at 20:55
4

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!");
    }
}
2
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.