用了PowerPolicyNotify和SystemIdleTimerReset, 还是一样的问题, 搞不明白该怎么处理, 麻烦大家帮我看看下面这个函数:
INT WINAPI App_PwrMonThread(void)
{
HANDLE hNotifications; //Handle to the power notification 'object'
MSGQUEUEOPTIONS msgOptions = {0};
bool fCapturing_Suspend = false;
WHY_UNATTENDED why_unattended = TO_SUSPEND;
BOOL unattended_added = FALSE;
HANDLE pwrThrEvent[2];
DWORD dwStatus;
//Prepare the options for message queue
msgOptions.dwSize = sizeof(MSGQUEUEOPTIONS);
msgOptions.dwFlags = 0;
msgOptions.dwMaxMessages = QUEUE_ENTRIES;
msgOptions.cbMaxMessage = sizeof(POWER_BROADCAST) + MAX_NAMELEN;
msgOptions.bReadAccess = TRUE;
//Create the message queue object that is needed to receive the notifications.
ghReadMsgQ = CreateMsgQueue(NULL, &msgOptions);
if ( NULL == ghReadMsgQ )
{
gbFlagExitThrd = TRUE;
}
//Create a unamed event object.
ghPWRSTOP_Event = CreateEvent(NULL, FALSE, FALSE, NULL);
pwrThrEvent[0] = ghReadMsgQ;
pwrThrEvent[1] = ghPWRSTOP_Event;
/* Request Power notifications*/
//To register for power notification events.
hNotifications = RequestPowerNotifications(ghReadMsgQ, POWER_NOTIFY_ALL);
if ( ! hNotifications )
{
gbFlagExitThrd = TRUE;
}
// increase count by 1 at initial
// Aeolus, Pwr, +
#if 0
if ( unattended_added == FALSE)
{
PowerPolicyNotify(PPN_UNATTENDEDMODE, TRUE);
unattended_added = TRUE;
DBG_MSG(MSG_INFO, L" PPN_UNATTENDEDMODE+\r\n");
}
// Aeolus, Pwr, +
#endif
//Power monitor thread function loop
while(!gbFlagExitThrd)
{
int iBytesInQueue = 0;
DWORD dwFlags;
UCHAR buf[QUEUE_SIZE];
PPOWER_BROADCAST pB = (PPOWER_BROADCAST) buf;
memset(buf, 0, QUEUE_SIZE);
DBG_MSG(MSG_INFO, L"PWM: Waiting for PM state transition notification ");
dwStatus = WaitForMultipleObjects(2, pwrThrEvent, FALSE, INFINITE);
switch(dwStatus)
{
case WAIT_OBJECT_0:
{
DBG_MSG(MSG_INFO, L"PWM: msg incoming");
}
break;
case WAIT_OBJECT_0+1:
{
DBG_MSG(MSG_INFO, L"PWM: ghPWRSTOP_Event been setted\r\n");
gbFlagExitThrd = TRUE;
continue;
}
break;
default:
{
DBG_MSG(MSG_INFO, L"default\r\n");
}
break;
}
//Retrieve the notification from the message queue.
if ( !ReadMsgQueue (ghReadMsgQ, buf, QUEUE_SIZE, (LPDWORD)&iBytesInQueue, INFINITE, &dwFlags) )
{
DBG_MSG(MSG_ERR, L"PWM: ReadMsgQueue: ERROR:%d", GetLastError());
gbFlagExitThrd = true;
}
else if ( iBytesInQueue < sizeof(POWER_BROADCAST) )
{
DBG_MSG(MSG_INFO,
TEXT("PWM: Received short message: %d bytes, expected: %d"),
iBytesInQueue,
sizeof(POWER_BROADCAST));
}
else
{
DBG_MSG(MSG_INFO, L"PWM: ReadMsgQueue");
//Call the appropriate function from the document when the appropriate notification was read
switch ( pB->Message )
{
case PBT_TRANSITION:
{
switch ( POWER_STATE(pB->Flags) )
{
case POWER_STATE_UNATTENDED:
{
DBG_MSG(MSG_INFO, L"PWM: Unattended");
switch (why_unattended)
{
case TO_SUSPEND:
{
DBG_MSG(MSG_INFO, L"PWM: Unattended - TO_SUSPEND");
//Hang up the VT call
if (g_bfAnswer || g_bfMakeVTCall)
{
DBG_MSG(MSG_INFO, L"PWM: Hang up VT call");
DBG_MSG(MSG_INFO, L"Before Kill SUSPEND_TIMER");
KillTimer(g_hwndVideoDlg, IDC_BACKLIGHT_SUSPEND_TIMER);
DBG_MSG(MSG_INFO,(L"MobiVtAPP -- WM_USER_DISCONNECTED: hWnd %X"), g_hwndVideoDlg);
g_bfUserHang = TRUE;
// Aeolus, Pwr, +
//if (g_bfIsLibInitiated)
if (g_bfVTCall)
{
DBG_MSG(MSG_INFO, L"** END VT CALL **");
PostMessage(g_hwndVCC, WM_END_VTCALL, NULL, NULL);
}
else
{
g_bfDisConnectedBeforeDoCall = TRUE;
}
// Aeolus, Pwr, -
}
// Aeolus, Pwr, +
#if 0
if ( unattended_added == TRUE)
{
DBG_MSG(MSG_INFO, L"PWM: Unattended - TO_SUSPEND PPN_UNATTENDEDMODE FALSE\r\n");
PowerPolicyNotify(PPN_UNATTENDEDMODE, FALSE);
DBG_MSG(MSG_INFO, L"PPN_UNATTENDEDMODE-\r\n");
unattended_added = FALSE;
}
#endif
// Aeolus, Power, -
}
break;
default:
break;
}
}
break;
default:
{
DBG_MSG(MSG_INFO, TEXT("PWM: Unknown Power State Flags:0x%x\r\n"),pB->Flags);
}
break;
}//END - switch ( POWER_STATE(pB->Flags) )
}
break;
case PBT_RESUME:
{
DBG_MSG(MSG_INFO, L"PWM: PBT_RESUME\r\n");
}
break;
default:
{
DBG_MSG(MSG_INFO, TEXT("PWM: Unknown Message:%d\r\n"), pB->Message);
}
break;
}//End- switch ( pB->Message )
}
} //End-while(!gbFlagExitThrd)
DBG_MSG(MSG_INFO, L"PWM: left msg loop\r\n");
// Aeolus, Pwr, +
#if 0
if ( unattended_added == TRUE)
{
PowerPolicyNotify(PPN_UNATTENDEDMODE,FALSE);
DBG_MSG(MSG_INFO, L"PWM: setted PPn false\r\n");
DBG_MSG(MSG_INFO, L"PPN_UNATTENDEDMODE-\r\n");
}
// Aeolus, Pwr, -
#endif
//Deregister from power notification.
if ( hNotifications )
{
StopPowerNotifications(hNotifications);
hNotifications = NULL;
DBG_MSG(MSG_INFO, L"PWM: StopPowerNotifications\r\n");
}
//Close the message queue.
if ( ghReadMsgQ )
{
if (!CloseMsgQueue(ghReadMsgQ))
{
DBG_MSG(MSG_ERR, L"PWM: CloseMsgQueue fail");
}
else
{
ghReadMsgQ = NULL;
DBG_MSG(MSG_INFO, L"PWM: CloseMsgQueue");
}
}
return 0;
}