如何以编程方式防止Windows从硬盘驱动器降速下来? [英] How to programatically prevent Windows from hard disk drive spin down?
问题描述
我的程序在硬盘可用空间上执行任务。
任务很长,需要1-2个小时。
My program performs a task on the hard disk free space. The task is quite long, it takes 1-2 hours.
问题是笔记本电脑上的硬盘可能是几分钟后用户处于非活动状态时关闭。
The problem is that on laptop the hard disk may be turned off after few minutes when the user is inactive.
如何以编程方式防止Windows从硬盘旋转(关机)?
推荐答案
为防止系统进入空闲模式,您可以尝试使用 SetThreadExecutionState
功能。此功能通知系统该应用程序正在使用中,并允许您指定线程的执行要求。用法可以像这样,但是我不确定这是否还会影响磁盘关机计时器:
To prevent the system from entering idle mode you may try to use the SetThreadExecutionState
function. This function informs the system that the application is in use and allows you to specify the thread's execution requirements. The usage can be like this, but I'm not sure if this affects also the disk power down timer:
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;
或者还提供了一组新的函数 PowerCreateRequest
, PowerSetRequest
和 PowerClearRequest
是专为Windows 7设计的,但是文档令人困惑,在此我还没有找到它们的用法示例时间。
Or there is also available the new set of functions PowerCreateRequest
, PowerSetRequest
and PowerClearRequest
designed for Windows 7, but the documentation is confusing and I haven't found any example of their usage at this time.
或者您可以通过 PowerWriteACValueIndex
或 PowerWriteDCVal ueIndex
与电源设置的 GUID_DISK_SUBGROUP
子组一起使用。
Or you can modify the power settings by PowerWriteACValueIndex
or PowerWriteDCValueIndex
functions with the GUID_DISK_SUBGROUP
subgroup of power settings.
这篇关于如何以编程方式防止Windows从硬盘驱动器降速下来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!