使用windows api 喚醒睡眠的系統(win7)

來源:互聯網
上載者: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 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 the
SetWaitableTimer function. The pDueTime parameter specifies when the timer will be signaled. To specify that the system should wake when the timer is signaled,set the
fResume 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);
 }
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.