58

I run Matlab simulations that sometimes take weeks to finish and when Windows restarts I lose all my work so I really need a way to stop auto restarts.

Windows 11 seems to have disabled all ways to get around Auto Update Restarts. Is there a still a workaround?

It used to be possible to set NoAutoRebootWithLoggedOnUsers in:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU

if you also set AUOptions to 4 in the same location but this does not work for me now.

17
  • 7
    1. Make sure there are no pending updates before you start, then, 2. Disconnect the PC from the internet. On Windows 10 machine, one can set Ethernet, as well as WiFi, metered (winhelponline.com/blog/…), but on Win 11, one might need to use Airplane Mode. [When "metered" is used, updates on Win 10 are not automatic, but internet is available.] Commented May 27 at 15:38
  • 44
    Matlab works on Linux too... more workaround than this ! :-). 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...
    – Hastur
    Commented May 27 at 17:47
  • 8
    @Hastur. Yes, its a good reason to say bye bye to Windows. I would love to be able to save a restart file but they are usually very complex simulations with lots of complex states that it would be very difficult to get back to without running from the start again. Commented May 27 at 17:53
  • 12
    Another workaround. Run the simulation in a virtual machine. Set the virtual machine to freeze (save state on the hdd) when receives the shutdown and to restart automatically after the reboot. Increase the grace time on the host for the reboot to allow the guest machine to save the status. You should not lose more than 10% of the time. I always run simulations on linux/BSD machine...
    – Hastur
    Commented May 27 at 17:53
  • 7
    @MichaelMcLaughlin My question has received 3 downvotes. Is there any way to find out why? I don't know for sure, but I suspect those downvotes were from people who think that you should not be deferring updates no matter what, people who feel that updates are so important that they outweigh actually getting work done. (And yes, I know, updates are important, because security vulnerabilities are Bad. The question is, of course, what balance to strike between uptime and safety.) Commented May 28 at 21:45

3 Answers 3

26

Windows will not reboot during working hours. My solution is to run a scheduled task that changes the working hours in the registry, say every hour or two, so that it is never not a working hour. I coded up this 4 line cmd command batch file program:

for /f %%i in ('powershell "((get-date).Hour+18) %% 24"') do set startHour=%%i
for /f %%i in ('powershell "((get-date).Hour+12) %% 24"') do set endHour=%%i

reg add HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings /v ActiveHoursStart /t REG_DWORD /d %startHour% /f 
reg add HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings /v ActiveHoursEnd /t REG_DWORD /d %endHour% /f  

I then used Windows Task Scheduler to make this .bat script run every hour.

Task Scheduler

Tested: It works! I installed an update requiring a restart on 31-May at 3pm UTC. I am informed that this restart will happen outside of working hours. As of 19-June at 6pm UTC, nearly 21 days later, no restart had taken place.

I figured out how to incorporate @Jonathan's feedback. It is easier to do modulo maths (and to get the hour independent of locale/region/format preferences) with powershell commands. It's still a cmd batch file, but it calls powershell. Reduces to 4 lines and should work everywhere.

14
  • 13
    Cool hack! Please let us know whether it works... BTW, you can do the hour logic much more easily in PowerShell: $currentHour = (get-date).Hour; $startHour = ($currentHour+18) % 24; $endHour = ($currentHour+6) % 24
    – Jonathan
    Commented May 28 at 12:01
  • I tried out this script and it seems to work well, just two problems: 1) it doesn't work in the AM if the time format is HH, because it doesn't recognise, say, 08 as an int to add. 2) Evil Windows Update has now started inserting Scheduled Tasks to wake up my PC and reboot it at the end of the active hours, so it won't stay asleep anymore!
    – Coxy
    Commented May 30 at 23:17
  • 1
    @MichaelMcLaughlin huh, weird that yours works and mine doesn't! 08 is not GTR 5 for me, then it tries to run set /a startHour = 08+18 and fails with Invalid number error.
    – Coxy
    Commented May 31 at 3:54
  • @Coxy correct, 08 and 09 are invalid octal numbers. Just run set /a 08 or set /a 09 to see. You always need to drop the leading zeros for it to work
    – phuclv
    Commented May 31 at 4:52
  • 1
    First, excellent work, this is simplicity and efficiency. Second, screw Microsoft for making us have to jump through hoops to keep Updates from happening when WE need them to.
    – OldTimer
    Commented Jun 8 at 6:02
16

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

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)

7
  • I don't think this particular API will be all that helpful. Note that ShutdownBlockReasonCreate simply sets the string that shows up in the shutdown UI (the one pictured in your screenshot); calling the method does not actually prevent shutdown from occurring. To prevent shutdown, you have to have an open window and return FALSE when you receive WM_QUERYENDSESSION. However, even if you do that, it's my impression that Windows Update gets aggressive and eventually forces a restart anyway, ignoring apps that try to prevent it.
    – josh3736
    Commented May 28 at 10:37
  • 3
    @josh3736 My program does return FALSE on receiving WM_QUERYENDSESSION. I haven't been using it recently, but about one year ago, it could block an update that happened at midnight all the way until I woke up in the morning. Do not know if Microsoft will play some evil trick to force the restart continue anyway after a longer time. Commented May 28 at 10:45
  • 1
    Right, the WM_QUERYENDSESSION return value is the part that actually prevents shutdown. I'm just pointing out that ShutdownBlockReasonCreate is entirely informational, and contrary to what this answer says, calling it does not actually do anything to prevent a system shutdown.
    – josh3736
    Commented May 28 at 11:10
  • 3
    That said, I know that returning false from WM_QUERYENDSESSION won't prevent a reboot indefinitely either. Eventually, Windows Update will force a restart anyway, which is why there's so much complaining about this on the internet.
    – josh3736
    Commented May 28 at 11:14
  • 1
    What may make this less useful is the small detail that seeing a message about one process blocking a restart doesn't necessarily mean that the other processes - including Matlab - aren't gone already. Win10 usually has a Cancel button on this screen letting you go back and handle the application yourself, and then you can see that everything else is closed.
    – tevemadar
    Commented May 30 at 9:23
15

The Group Policy Configure Automatic Updates allows you to prevent automatic installation of updates.

  1. Open gpedit.msc
  2. Computer -> Administrative Templates -> Windows Components -> Windows Update -> Manage end user experience -> Configure Automatic Updates (Alternatively go through "all settings" if you use English and you know the name from here)
  3. Enable, Set to (2) notify only

Specifies whether this computer will receive security updates and other important downloads through the Windows automatic updating service.

This setting lets you specify whether automatic updates are enabled on this computer. If the service is enabled, you must select one of the four options in the Group Policy Setting:

2 = Notify before downloading and installing any updates.

When Windows finds updates that apply to this computer, users will be notified that updates are ready to be downloaded. After going to Windows Update, users can download and install any available updates.

7
  • 7
    Is GP still limited to "Pro" versions and not available on "Home" (assuming that silliness still exists in Windows 11)?
    – millebi
    Commented May 28 at 15:08
  • 4
    My understanding is that even Pro editions of Windows will ignore this Group Policy settings. The OP would have to pirate an Enterprise edition of Windows.
    – Coxy
    Commented May 28 at 23:19
  • 1
    @Coxy IME that's not true. The policy works fine on Pro.
    – Bob
    Commented May 29 at 6:22
  • 1
    @millebi Yes, the Group Policy Editor is not available in the Home editions of Windows, but from Professional upwards. The Group Policy Editor is a frontend to the registry settings though, so you should be able to set the appropriate registry directly as an alternative on Home editions.
    – Kissaki
    Commented May 29 at 13:26
  • @Coxy That setting specifically, or any setting that is listed in group policies? Why would Professional have the editor for it then?
    – Kissaki
    Commented May 29 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .