How to programatically prevent Windows from hard disk drive spin down? How to programatically prevent Windows from hard disk drive spin down? windows windows

How to programatically prevent Windows from hard disk drive spin down?


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;

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.

Or you can modify the power settings by PowerWriteACValueIndex or PowerWriteDCValueIndex functions with the GUID_DISK_SUBGROUP subgroup of power settings.


Windows does not allow applications to disable power control changes, because buggy applications were causing batteries to be drained. See http://blogs.msdn.com/b/oldnewthing/archive/2007/04/16/2148139.aspx

You can get notified when the system power status is about to be changed. See WM_POWERBROADCAST Messages.