Windows actually has an API that enables a process that has a window to block a system shutdown indefinitely until the user manually chooses what to do next. (possibly, as @josh3736
pointed out, Windows Update may break this eventually. But I personally have not experienced a concrete case of it. At least it should be safe to block a shutdown when you sleep until you wake up, and then you just click cancel to cancel the shutdown)
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-shutdownblockreasoncreate
ShutdownBlockReasonCreate
function (winuser.h)
Indicates that the system cannot be shut down and sets a reason string to be displayed to the user if system shutdown is initiated.
Note that calling this alone cannot block a shutdown/restart successfully. You also need your application to handle the WM_QUERYENDSESSION
and WM_ENDSESSION
messages. I will discuss them at the end.
If you are not a developer so you do not know how to use Windows API, you can use a small program that I have written years ago for exactly the same purpose: https://github.com/real-guanyuming-he/ShutdownBlocker
Either way, when you have successfully created a shutdown block reason, Windows will pause when a shutdown (including an update restart) is issued, and display this to you, where you can click Cancel to cancel the shutdown.
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/26FNWogM.jpg)
Note: If you have no control over your Matlab simulations program (I do not use Matlab so I do not know if it is possible), then it might or might not work, depending on how Matlab handles the situation. According to Microsoft (https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down),
Applications with a window and message queue receive shutdown notifications through the WM_QUERYENDSESSION
and WM_ENDSESSION
messages. These applications should return TRUE to indicate that they can be terminated.
If Matlab chooses to return TRUE on receiving this message, it will close, even if the shutdown is blocked this way. (And if you call the API without returning FALSE
on receiving WM_QUERYENDSESSION
, then it will not succeed as your app would close as a result of it)
:-)
. By the way, for simulations that run for a long time it is common practice to dump the data and save the restart file every xx minutes, where xx is the amount of (simulation) time you are willing to lose. Usually we go by trial and error to translate the simulation time (MD) or the number of steps (MC) into human time...