Best way to detect an application crash and restart it?

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)

Leave a Comment