我们致力于在Windows中运行的“任务关键型”软件.一般情况下,如果Windows自动更新中断我们的流程是很糟糕的,因为它可能意味着报废材料中的损失(您无法停止和恢复以后,工作必须从头到尾不间断地工作).
在过去,我们能够通过让我们的软件安装程序在Windows注册表中设置一个参数(在用户安装程序的同意下)来防止在用户登录时自动更新后自动重启.我们能够解决这个问题.对于自动更新,将通知用户存在需要重新启动的更新并且在准备好它时单击按钮.这适用于Windows Vista,7和8 / 8.1.但是,对于最新的Windows 10(我正在使用Creators的更新),该参数似乎不再有效,因为我观察到我的计算机经历了自动更新,我知道该注册表项已经生效.
在我的研究中,我发现似乎是一个希望的地方,可以选择一个设置,其中Windows将为用户提供安排更新的机会,而不是仅在Windows认为合适时自动执行(信息here) .但是,我不确定如何以编程方式设置Windows,以便安排更新的选项成为默认选项.
是否有一种编程方式来设置Windows 10(最好以友好的方式),以便它不会自动重启?
解决方法
API文档引用CD刻录作为示例,但同样适用于您的“任务关键型”过程.
Applications should call this function as they begin an operation that cannot be interrupted,such as burning a CD or DVD. When the operation has completed,call the ShutdownBlockReasonDestroy function to indicate that the system can be shut down.
请注意,该文档专门引用了用户关闭,但我不明白为什么它也不应该也适用于更新重启.
根据您的评论,您似乎需要使用Windows API例程的帮助.我建议你在适当的库中声明外部函数. (但你可以毫无顾虑地在同一个单位进行测试.)
function ShutdownBlockReasonCreate(hWnd: HWND; Reason: LPCWSTR): BOOL; stdcall; external user32; function ShutdownBlockReasonDestroy(hWnd: HWND): BOOL; stdcall; external user32;
以下演示了如何使用API.注意:注意错误检查.我已经演示了如何获取错误信息.你用它做什么取决于你.
要指出的另一个重要的事情(在评论中重复)是你不应该阻止主线程.有关详细信息,请参阅Vista here中首次引入这些更改时的Microsoft文档.
procedure TForm1.JobStartClick(Sender: TObject); var LErr: Cardinal; begin ListBox1.Items.Add('Attempting to block shutdown:'); if (not ShutdownBlockReasonCreate(Application.MainForm.Handle,'Super Critical Job')) then begin LErr := GetLastError; ListBox1.Items.Add('... Failed: ' + SysErrorMessage(LErr)); //Probably not safe to start your job in this case,but perhaps you //choose to give it a shot anyway. Exit; end; ListBox1.Items.Add('... success'); FJobRunning := True; //Start the job. //However,NB do not run the job here. //If it takes a long time and is not asynchronous,you should probably //run your job on a separate thread. ***Do not block the main thread // otherwise Windows will still kill your app for not responding*** end; procedure TForm1.JobEndClick(Sender: TObject); var LErr: Cardinal; begin if (not FJobRunning) then Exit; //End the job. //Again,do not block the main thread,so perhaps this is rather something //to do after you already know the job is done. FJobRunning := False; ListBox1.Items.Add('Allow shutdown'); if (not ShutdownBlockReasonDestroy(Application.MainForm.Handle)) then begin LErr := GetLastError; ListBox1.Items.Add('... Failed: ' + SysErrorMessage(LErr)); end; end; //Declare the handler for the WM_QUERYENDSESSION message as follows. //procedure WMQueryEndSession(var AMsg : TWMQueryEndSession); message WM_QUERYENDSESSION; procedure TForm1.WMQueryEndSession(var AMsg: TWMQueryEndSession); begin ListBox1.Items.Add('WMQueryEndSession'); if (FJobRunning) then //NB: This is very important. //You still need to confirm that your application wants to block //shutdown whenever you receive this message. AMsg.Result := 0 else inherited; end;