Skip to content

Instantly share code, notes, and snippets.

@hyrious
Last active March 18, 2018 12:49
Show Gist options
  • Save hyrious/0f40d1cae92229d6411095d7b57a54e5 to your computer and use it in GitHub Desktop.
Save hyrious/0f40d1cae92229d6411095d7b57a54e5 to your computer and use it in GitHub Desktop.
prevent sleep
#include <windows.h>
#include <stdio.h>
int running;
BOOL WINAPI consoleHandler(DWORD signal) {
if (signal == CTRL_C_EVENT) {
running = FALSE;
return TRUE;
}
return FALSE;
}
int main(int argc, char const *argv[])
{
running = TRUE;
if (!SetConsoleCtrlHandler(consoleHandler, TRUE)) {
puts("ERROR: Could not set control handler");
return 1;
}
SetThreadExecutionState(0x80000041);
puts("I will not sleep until you Ctrl-C.");
while (running && getchar() != EOF) {
Sleep(0);
}
SetThreadExecutionState(0x80000000);
puts("I'm sleepy now.");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment