为防止系统进入空闲模式,您可以尝试使用
SetThreadExecutionState
功能.该函数通知系统应用程序正在使用,并允许您指定线程的执行要求.用法可以这样,但我不知道这是否也会影响磁盘掉电定时器:
type EXECUTION_STATE = DWORD; const ES_SYSTEM_required = $00000001; ES_DISPLAY_required = $00000002; ES_USER_PRESENT = $00000004; ES_AWAYMODE_required = $00000040; ES_CONTINUOUS = $80000000; function SetThreadExecutionState(esFlags: EXECUTION_STATE): EXECUTION_STATE; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState'; procedure TForm1.Button1Click(Sender: TObject); begin if SetThreadExecutionState(ES_CONTINUOUS or ES_SYSTEM_required or ES_AWAYMODE_required) <> 0 then try // execute your long running task here finally SetThreadExecutionState(ES_CONTINUOUS); end; end;
或者也可以使用为Windows 7设计的新功能PowerCreateRequest
,PowerSetRequest
和PowerClearRequest
,但是文档令人困惑,我目前还没有找到任何使用示例.
或者您可以使用GUID_DISK_SUBGROUP电源设置子组修改PowerWriteACValueIndex
或PowerWriteDCValueIndex
功能的电源设置.