使用windows api 唤醒睡眠的系统(win7)

来源:互联网 发布:如何使用淘宝冰点营销 编辑:程序博客网 时间:2024/04/29 11:50

 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 as awake-up event. Use a waitable timer object to specify the time at which the system should wake. To create the object, use theCreateWaitableTimer function. To set the timer, use theSetWaitableTimer function. The pDueTime parameter specifies when the timer will be signaled. To specify that the system should wake when the timer is signaled,set thefResume parameter 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 to call the SetThreadExecutionState function 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 call toSetThreadExecutionState.
  • 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 the 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 should do nothing. Event-handling applications, such as fax servers, should handle their events. To determine whether the system is in this state, call theIsSystemResumeAutomatic 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 should reopen files that it closed when the system entered the sleep state, and it should prepare for user input.

Windows Server 2003, Windows XP, and Windows 2000:  If an application calledSetSystemPowerState withfForce set to TRUE or the system performed 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:%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, NULL, 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);
 }
 

原创粉丝点击