Arun
Arun

Reputation: 2373

Prevent Windows 10/11 from going into modern standby mode using C++ application?

I need to delay modern standby mode until my application performs some operation. Currently I am capturing modern standby event using EvtSubscribe and in the callback function, my application performs some operation. But sometimes the event callback called during modern standby exit. I suspect OS suspend the application activity and resumes after modern standby exit. Is there any solution available to delay OS to enter into modern standby?

LPCWSTR pwsPath = L"System";
LPCWSTR pwsQuery = L"(*[System[(EventID=\"####\")]])"; // #### will be replaced with actual event in the original code

_ghCSEventsSubscription = EvtSubscribe(NULL, NULL, pwsPath, pwsQuery, NULL, NULL,
        (EVT_SUBSCRIBE_CALLBACK)CSEventsSubscriptionCallback, EvtSubscribeToFutureEvents);

Upvotes: 2

Views: 439

Answers (1)

Douglas B
Douglas B

Reputation: 802

I am not positive if this is appropriate for your use case, however I have had success using SetThreadExecutionState() (more info) to keep a system alive and logged in while other tasks run in the background which could not (the system in question has security preventing disabling sleep/logout mode)

#include <iostream>
#include <Windows.h>
int main()
{
    SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);
    system("pause");
    return 1;
}

Please let me know if this helps you, or if your issue is different than I am understanding.

Upvotes: 5

Related Questions