Use Windows API to wake up a sleep system (win7)

Source: Internet
Author: User

System wake-up events:

Your application can restore an onnow-capable computer that is in a sleep state to the working state by using a scheduled timer or a device event. This is known asWake-up event. Use a waitable timer object to specify
The time at which the system shoshould wake. To create the object, useCreatewaitabletimerFunction. To set the timer, use
SetwaitabletimerFunction.PduetimeParameter specifies when the timer will be signaled. to specify that the system shoshould wake when the timer is signaled, set
FresumeParameter to true.

When the system wakes automatically because of an event (other than power switch or user activity), the system automatically sets an unattended idle timer to at least 2 minutes. This timer gives applications sufficient time
CallSetthreadexecutionstateFunction to indicate that they are busy. This time enables the system to return to the sleep state quickly after the computer
Is no longer required. The following criteria determine whether the system returns to the sleep state:

  • If the system wakes automatically (that is, no user activity is present), it shuts down as soon as the unattended idle timer expires and no applications indicate that the system is required via a callSetthreadexecutionstate.
  • If the system wakes automatically, but the user provides new input while the event is handled, the system will not automatically return to sleep Based on the unattended idle timer. instead the system will sleep based on
    System idle timer.
  • If the system wakes due to user activity, the system will not automatically return to sleep Based on the unattended idle timer. Instead the system will sleep based on the system idle timer.

When the system wakes automatically, the System Broadcasts thepbt_apmresumeautomatic event to all applications. Because the user is not present,
Most applications shocould do nothing. Event-handling applications, such as fax servers, shocould handle their events. To determine whether the system is in this State, callIssystemresumeautomatic
Function. When the system resumes automatically, the display is not automatically turned on.

If the system wakes due to user activity, the system will first broadcast the pbt_apmresumeautomatic event followed by apbt_apmresumesuspend
Event. In addition, the system will turn on the display. Your application shoshould reopen files that it closed when the system entered the sleep state, and it shoshould prepare for user input.

Windows Server 2003, Windows XP, and Windows 2000:If an application calledSetsystempowerstate
WithFforceSet to true or the system has med a critical suspend, the system will broadcast apbt_apmresumecritical event after waking.

Win32 Code as follows:

Handle htimer = NULL;
Large_integer liduetime;
Systemtime st;

Liduetime. quadpart =-300000000; // 30 seconds

// Create a waitable timer.
Htimer = createwaitabletimer (null, true, l "waitabletimer ");
If (null = htimer)
{
Printf ("createwaitabletimer failed (% d) \ n", getlasterror ());
Return 1;
}

Getlocaltime (& St );
// Printf ("% d: % d \ n", st. whour, st. wminute, st. wsecond );
Printf ("% 02d: % 02d: % 02d waiting for 30 seconds... \ n", st. whour, st. wminute, st. wsecond );

// Set a timer to wait for 30 seconds.
If (! Setwaitabletimer (htimer, & liduetime, 0, null, null,
True ))
// If (! Setwaitabletimerex (htimer, & liduetime, 0, null, 0 ))
{
Printf ("setwaitabletimer failed (% d) \ n", getlasterror ());
Return 2;
}

// Wait for the timer.

If (waitforsingleobject (htimer, infinite )! = Wait_object_0)
Printf ("waitforsingleobject failed (% d) \ n", getlasterror ());

If (! Setthreadexecutionstate (es_display_required | es_system_required ))
{
Printf ("setthreadexecutionstate failed (% d) \ n", getlasterror ());
}
Else
{
Getlocaltime (& St );
Printf ("% 02d: % 02d: % 02d timer was signaled. \ n", st. whour, st. wminute, st. wsecond );
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.