Se supone que el siguiente fragmento de código evita que una máquina con Windows entre en modo de suspensión mientras finaliza una operación importante (ejecutando un subproceso diferente):
[Flags] private enum EXECUTION_STATE : uint { ES_AWAYMODE_REQUIRED = 0x00000040, ES_CONTINUOUS = 0x80000000, ES_DISPLAY_REQUIRED = 0x00000002, ES_SYSTEM_REQUIRED = 0x00000001 } [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); public async Task KeepAwakeAsync(CancellationToken cancellationToken = default) { while (!cancellationToken.IsCancellationRequested) var result = SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED); // (XXX) if (result == 0) { throw new InvalidOperationException("SetThreadExecutionState failed."); } await Task.Delay(TimeSpan.FromMinutes(1)).ConfigureAwait(false); } } No estoy seguro de si también se necesita ES_CONTINUOUS (es decir, var result = SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED); ).
¿Alguien puede explicar la diferencia entre ES_CONTINUOUS | ES_SYSTEM_REQUIRED y ES_SYSTEM_REQUIRED cuando se usa en el ejemplo anterior?
Leí en alguna parte que SetThreadExecutionState se aplica solo al hilo actual. ¿Es eso cierto? ¿Alguien puede dar más detalles sobre el significado exacto?
He leído [1] y por alguna razón me confunde cada vez que lo leo.
Recursos: