Best way to detect an application crash and restart it?

13,941

Solution 1

How about creating a wrapper application that launches the faulty app as a child and waits for it? If the exit code of the child indicates an error, then restart it, else exit.

Solution 2

Best way is to use a named mutex.

  1. Start your application.
  2. Create a new named mutex and take ownership over it
  3. Start a new process (process not thread) or a new application, what you preffer.
  4. From that process / application try to aquire the mutex. The process will block
  5. When application finish release the mutex (signal it)
  6. The "control" process will only aquire the mutex if either the application finishes or the application crashes.
  7. Test the resulting state after aquiring the mutex. If the application had crashed it will be WAIT_ABANDONED

Explanation: When a thread finishes without releasing the mutex any other process waiting for it can aquire it but it will obtain a WAIT_ABANDONED as return value, meaning the mutex is abandoned and therfore the state of the section it was protected can be unsafe.

This way your second app won't consume any CPU cycles as it will keep waiting for the mutex (and that's enterely handled by the operating system)

Solution 3

I realize that you're dealing with Windows XP, but for people in a similar situation under Vista, there are new crash recovery APIs available. Here's a good introduction to what they can do.

Share:
13,941
Joan Cardona
Author by

Joan Cardona

I like wooden pencils.

Updated on July 28, 2022

Comments

  • Joan Cardona
    Joan Cardona almost 2 years

    What's the best way to detect an application crash in XP (produces the same pair of 'error' windows each time - each with same window title) and then restart it?

    I'm especially interested to hear of solutions that use minimal system resources as the system in question is quite old.

    I had thought of using a scripting language like AutoIt (http://www.autoitscript.com/autoit3/), and perhaps triggering a 'detector' script every few minutes?

    Would this be better done in Python, Perl, PowerShell or something else entirely?

    Any ideas, tips, or thoughts much appreciated.

    EDIT: It doesn't actually crash (i.e. exit/terminate - thanks @tialaramex). It displays a dialog waiting for user input, followed by another dialog waiting for further user input, then it actually exits. It's these dialogs that I'd like to detect and deal with.