Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

486
Views
¿Cómo llamar a SetThreadExecutionState correctamente?

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); } }
  1. 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?

  2. 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:

  • [1] https://docs.microsoft.com/en-gb/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
  • [2] Evite que la pantalla duerma con C#
  • [3] SetThreadExecutionState no funciona cuando se llama desde el servicio de Windows
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!