The power settings on a computer influence when it stays awake or goes into a lower power mode. User action influences this too. There are times when I want to start a long running process and walk away to come back to it later. Sometimes, I forget that unless I take some action (such as changing the power settings) the computer will go to sleep and halt my long-running work. I come back to find that the computer has been sleeping while I thought it was working. There have been times when I’ve played a muted video in a loop because I know the media player will keep the computer awake. I can restore normal power behaviour by just closing the media player. Since I run into this need enough, I thought it best to have an application dedicated to this need. I’ve made one that you can download and use if you’d like. Full source code is available along with a signed binary.
The source and binary are small. Let’s walk through the entire source code as it exists today.
#include <windows.h>
#include <iostream>
#include <string>
const int INDEFINITE_WAKE_TIME = -1;
struct Options
{
int wakeTime = -1;
bool verbose = false;
};
At the start of the source code, I include a few headers for functionality that I’ll need to use. I also declare a struct called Options. You may find structs of similar names in other code that I post. This struct is for holding onto the interpreted arguments that someone passed through the command line. Defaults are also provided. If no time length is specified for how long the program will keep the computer awake, it will do so infinitely (or until someone presses CTRL-C). By default, it will also not print anything to the output console. If it is asked to un in verbose mode, it will output text. The parsing of the arguments themselves are in the function ParseArguments().
void ParseArguments(int argc, wchar_t* argv[], Options& options)
{
for (int i = 0; i < argc; ++i)
{
std::wstring arg = argv[i];
auto nextArg = [&]() -> std::wstring {
if (i + 1 < argc)
{
return argv[i + 1];
}
return L"";
};
if ((arg == L"-w" || arg == L"--wake-time") && !nextArg().empty())
{
options.wakeTime = std::stoi(nextArg());
++i; // Skip the next argument since it's already processed
}
else if ((arg == L"-v") || (arg == L"--verbose"))
{
options.verbose = true;
}
else if ((arg == L"-s") || (arg == L"--silent"))
{
options.verbose = false;
}
else if ((arg == L"-i") || (arg == L"--indefinite"))
{
options.wakeTime = INDEFINITE_WAKE_TIME;
}
else if ((arg == L"-a")|| (arg == L"--about") || (arg == L"-h") || (arg == L"--help"))
{
std::wcout << L"Woke - A utility to prevent the system from sleeping.\n"
<< L"Usage:\n"
<< L" -w, --wake-time <seconds> Set a specific wake time in seconds.\n"
<< L" -i, --indefinite Keep the system awake indefinitely.\n"
<< L" -a, --about, -h, --help Display this information.\n";
exit(0);
}
}
}
I think this function is straight forward. It looks for specific strings in the command line arguments that are associated with commands. If it finds them, then it will set a field in the Options class accordingly. The one option are the arguments that request that the application display its help text. If that is invoked the application will print its text and terminate.
static std::atomic<bool> keepRunning{ true };
HANDLE waitEvent = NULL;
I declare a couple more variables. One variable, keepRunning, tracks whether the process has been asked to terminate or not. This variable currently isn’t of any consequence. It is in place for future intentions. The other variable, waitEvent, is important. WinAPI Events are operating system objects that are used for coordinate between threads (if unnamed) or processes (if named). This event will be unnamed. Named events are assigned a string ID. This one will not get such an ID, making it “unnamed.”
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
keepRunning = false;
SetEvent(waitEvent); // Signal the main thread to exit
return TRUE;
default:
return FALSE;
}
}
The function CtrlHandler is for shutting down the program if the user presses CTRL-C or CTRL-Break (among other close conditions). It updates the keepRunning variable and triggers the Event object.
The Entry Point
int wmain(int argc, wchar_t* argv[])
{
Options runOptions;
ParseArguments(argc, argv, runOptions);
SetConsoleCtrlHandler(CtrlHandler, TRUE);
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
waitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
DWORD waitTime = (runOptions.wakeTime == INDEFINITE_WAKE_TIME) ? INFINITE : runOptions.wakeTime * 1000;
if(runOptions.verbose)
{
std::wcout << L"System will be kept awake for " << (waitTime == INFINITE ? L"indefinitely" : std::to_wstring(waitTime / 1000) + L" seconds") << L". Press Ctrl+C to exit." << std::endl;
}
WaitForSingleObject(waitEvent, waitTime);
CloseHandle(waitEvent);
SetThreadExecutionState(ES_CONTINUOUS);
if (runOptions.verbose)
{
std::wcout << L"Woke has exited. System can now sleep." << std::endl;
}
}
Our entry point for this program is wmain. What separates wmain from main is that wmain receives the command line arguments as 16-bit characters instead of 8-bit characters. for programs that accept text data from a user you may want to use wmain in furtherance of better flexibility and support for multiple languages. In the first three function calls, we parse the command line and update the Options object, assign our handler for CTRL-C, and then call a call a WinAPI function to keep the computer awake. SetThreadExecutionStates() as called here only keeps the computer awake. Though the computer is awake, the display might go into sleep mode. passing the flag ES_DISPLAY_REQUIRED would also keep the screen awake.
The operating system event is created. Though the user passed how long to stay awake in seconds, we need this value in milliseconds. The call to WaitForSingleObject() accepts the event on which we are waiting and the maximum amount of time to wait. The thread will halt on that function call. It will resume once either the wait time has expired or the handler foor CTRL-C has triggered the event. When execution resumes the application deallocates the OS event, releases the demand to keep the computer awake, and it terminates.
Signed Code Executable
I see myself using this application frequently. I’ve found that Crowd Strike‘s security software has a tendency to give false positives to small executables. It is less prone to doing so if the application is signed. I’ve signed a build of this EXE so that I can use it without it being flagged as malware and having to fill out paperwork about the origins of the executable (true story, this happened to me once when I made a “Hello World” program). It is in the ./bin folder of the GitHub repository.
Posts may contain products with affiliate links. When you make purchases using these links, we receive a small commission at no extra cost to you. Thank you for your support.
Mastodon: @j2inet@masto.ai
Instagram: @j2inet
Facebook: @j2inet
YouTube: @j2inet
Telegram: j2inet
Bluesky: @j2i.net








































