June 8th, 2023

How can I register a program to auto-relaunch if it crashes or is terminated?, redux

Some time ago, I wrote on the topic of auto-restarting a program when it crashes or terminates. If your program is a service, you can set the service restart policy, which will restart the service if it crashes. I mentioned that there is no similar feature for regular applications. Turns out I was wrong.

The Register­Application­Restart function lets you specify a command line to use if the current process crashes or hangs, or if the program had been running at the time the system restarted to install an update. You can also specify flags that modify the default restart conditions.

Note that if the program crashes or hangs, the program does not restart automatically. The system informs the user of the crash or hang, and then asks if they want to restart the program. Only if the user agrees does the system execute the restart command. Note also that the restart command is not executed if the user manually terminates the program, such as by using Task Manager. The idea of the restart command is to make it easier for the user to recover their work, not to give programs an easy means of persistent execution.

Bonus chatter: To avoid infinite restart loops, both service auto-restart and process auto-restart have minimum uptime requirements before they will restart the crashed process. You can read the documentation for details.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

8 comments

Discussion is closed. Login to edit/delete existing comments.

  • sugrob 9000

    So there’s the ¹ that hints at a footnote, but neither here nor the linked article has one. I really wanted the extra details!

  • Danielix Klimax

    OT: Could you please kick whoever is in charge of devblogs to fix search? it’s nearly impossible to find anything on your blog. (Either 0 or too few results) I tried feedback, but no improvement…

    Thanks.

  • Georg Rottensteiner

    Not exactly the same, but is this Auto Restarting also affecting the restart of a program after a update reboot?
    You know, when you do a restart, and Windows tries to restart all the programs that were running when you clicked on it.

    This restarting also infers that programs should be able to do a quick save and reload on restart of its current state (opened documents, etc.)

    Some programs (e.g. Contoso Alphanumeric Slab) unfortunately are not...

    Read more
    • Scarlet Manuka · Edited

      From the article: "... or if the program had been running at the time the system restarted to install an update"

      The point of this is to give applications a way to be prepared for a suden reboot and survive it with work intact. It's up to the applications that make use of it to make sure they are actually fully prepared, and some certainly don't do a great job.

      But even the ones that do a...

      Read more
  • Dan Bugglin · Edited

    Ahh I wondered how that worked! I guess I thought it was magic Microsoft-only functionality. You know, the kind of stuff that would cause EU courts to throw a fit if they found out about it.

    Alright, MSDN says the API can restart an application if it is "updated" (an application update, OS updates are mentioned separately). What exactly would trigger this? Clearly if I just use a third-party installer like NSIS Windows wouldn't really see...

    Read more
    • Kalle Niemitalo

      Third-party installers can use the Restart Manager API.