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;
2条答案
按热度按时间vxqlmq5t1#
为了防止系统进入空闲模式,你可以尝试使用
SetThreadExecutionState
函数。这个函数通知系统应用程序正在使用中,并允许你指定线程的执行要求。用法可以是这样的,但我不确定这是否也会影响磁盘断电计时器:或者也有为Windows 7设计的一组新函数
PowerCreateRequest
、PowerSetRequest
和PowerClearRequest
,但文档令人困惑,目前我还没有找到它们用法的任何示例。或者,您可以通过
PowerWriteACValueIndex
或PowerWriteDCValueIndex
函数修改电源设置,这些函数具有电源设置的GUID_DISK_SUBGROUP
子组。jhiyze9q2#
Windows不允许应用程序禁用电源控制更改,因为有错误的应用程序导致电池耗尽。请参阅http://blogs.msdn.com/oldnewthing/archive/2007/04/16/2148139.aspx
当系统电源状态即将更改时,您可以收到通知。请参阅WM_POWERBROADCAST Messages。