问题:是否有一种编程方法可以防止Windows10在更新后自动重新启动?
我们的工作是运行在Windows上的“关键任务”软件。一般来说,如果Windows自动更新中断了我们的进程,这是很糟糕的,因为它可能意味着在报废的材料中损失了钱(您不能停止以后继续工作,工作必须从头到尾不间断地工作)。
在过去,我们可以通过让我们的软件安装程序在Windows中设置一个参数(征得用户-安装程序的同意)来避免在用户登录时自动更新后自动重新启动。因此,将通知用户需要重新启动更新,并在准备就绪时单击按钮,而不是自动更新。这适用于Windows、7和8/8.1。但是,对于最新的Windows 10 (我正在处理创建者更新),该参数似乎不再起作用了,因为我已经观察到我的计算机经历了一个自动更新,其中我知道注册表项已经生效。
在我的研究中,我发现了一个有希望的地方,在那里可以选择一个设置,让用户有机会安排更新,而不是在Windows认为合适的时候自动进行更新(信息这里)。但是,我不知道如何以编程方式设置Windows,以便将计划更新的选项变为默认选项。
是否有一种编程方式来设置Windows 10 (最好是友好的方式),使其不会自动重新启动?
发布于 2017-07-13 23:41:06
尝试关闭块原因API。ShutdownBlockReasonCreate
API文档以CD刻录为例,但这也适用于您的“关键任务”过程。
应用程序应该调用此函数,因为它们开始了无法中断的操作,例如刻录CD或DVD。操作完成后,调用ShutdownBlockReasonDestroy函数以指示系统可以关闭。
注意,这些文档专门引用了用户关机,但我不明白为什么它不应该也适用于更新重新启动。
NB:记住要检查函数是否成功,并在进程完成时销毁关闭原因。
根据您的评论,您似乎需要帮助使用Windows例程。我建议您在适当的库中声明外部函数。(但您可以在同一个单元中进行测试,而无需担心。)
function ShutdownBlockReasonCreate(hWnd: HWND; Reason: LPCWSTR): BOOL; stdcall; external user32;
function ShutdownBlockReasonDestroy(hWnd: HWND): BOOL; stdcall; external user32;下面演示如何使用API。注意:注意错误检查。我已经演示了如何获得错误信息。你用它做什么取决于你自己。
要指出的另一件重要的事情(在注释中重复)是,您不应该阻止主线程。有关更多信息,请参考微软在Vista 这里中首次引入这些更改时的文档。
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;发布于 2017-07-13 15:06:42
注册表项HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings包含两个条目:ActiveHoursStart和ActiveHoursEnd。根据需要在程序中更改这些条目,以禁止重新启动。这样,您可以控制重新启动,以避免发生时,您的程序运行。请注意,要更改这些设置,需要提升特权。
https://stackoverflow.com/questions/45083525
复制相似问题