Aria2: Add ability to prevent system from entering sleep/hibernation while downloading

Created on 30 Sep 2016  路  4Comments  路  Source: aria2/aria2

My Windows systems (desktops and battery-powered devices) are set to automatically sleep/hibernate on user inactivity after a specified time period. All the well-coded download managers, media players, disc burners, presentation software etc. that I use nowadays are able to deny the system from entering sleep/hibernation while they are busy executing user-initiated tasks, and so I don't have to tinker with my power settings unnecessarily. Windows has long provided power management APIs to enable such use cases.

Currently however I have to either remember to manually modify my active power plan both before _and_ after using aria2 (which is naturally cumbersome and often leads to an undesirable system state when I forget either of the two), or I need to use a batch file every time that automates the before/after power plan edits using _powercfg_ or similar (which is painful as well).

It would thus be _highly_ desirable if aria2 itself could invoke the standard OS power management APIs, exposed to end users via something like a --keep-system-awake-till-done option. The program's documentation doesn't seem to mention which versions of Windows it works on, but if XP and Vista are no longer supported then the enhanced PowerCreateRequest/PowerSetRequest/PowerClearRequest APIs can be used, otherwise the still supported SetThreadExecutionState API can be relied upon.

Further detailed information about the APIs is available from the usual sources, including MSDN of course and specifically the Power Availability Requests document available here (the _New Functions for Availability Requests_ section therein should help).

Thanks for reading, and I hope this feature will be implemented in a future version of this wonderful utility.

enhancement win

Most helpful comment

Really useful and i think this is an important feature for the future.

All 4 comments

@nmaier Are you interested in this?

Really useful and i think this is an important feature for the future.

If using the older SetThreadExecutionState() API, the skeleton code might look like:

#ifdef _WIN32
namespace {
// Call once on startup
void win32PreventSleep(void)
{
  SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
}

// Call once on shutdown (or ignore and let the OS clear the flag on process exit/crash
void win32AllowSleep(void)
{
  SetThreadExecutionState(ES_CONTINUOUS);
}
}
#endif

@0xABD, according to MSDN:

ES_AWAYMODE_REQUIRED should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping.

ES_SYSTEM_REQUIRED should be quite enough to prevent a computer from sleeping.

Was this page helpful?
0 / 5 - 0 ratings