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.
So there’s the ¹ that hints at a footnote, but neither here nor the linked article has one. I really wanted the extra details!
Sorry, that was a leftover footnote marker. I decided to promote the footnote to a Bonus chatter and forgot to remove the footnote marker.
https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-changeserviceconfig2w
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.
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...
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...
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...
Third-party installers can use the Restart Manager API.