CeSetUserNotificationEx无法在指定的时间运行?

iyond 2010-03-05 08:43:28
我写了个程序,是在手机进入休眠状态4分钟后唤醒。当获取到系统即将进入休眠时,通过调用CeSetUserNotificationEx来设置一个定时器,指定4分钟后运行一个程序。然后在系统Resuming的时候解除这个定时器。

现在有个问题是,这个程序大部分时间运行正常,但是有时候没有在指定的时间运行,而是延迟了好长时间。



下面是程序运行日志,各位注意标红的地方:
[2010-3-4 9:54:42][Nosleep]sleeping now, will add timer. next run time:2010-3-4 9:58:42
[2010-3-4 9:58:42][Nosleep]resuming, clear the timer
[2010-3-4 9:58:57][Nosleep]sleeping now, will add timer. next run time:2010-3-4 10:2:57
[2010-3-4 10:2:57][Nosleep]resuming, clear the timer
[2010-3-4 10:3:12][Nosleep]sleeping now, will add timer. next run time:2010-3-4 10:7:12
[2010-3-4 10:7:12][Nosleep]resuming, clear the timer
[2010-3-4 10:7:27][Nosleep]sleeping now, will add timer. next run time:2010-3-4 10:11:27
[2010-3-4 10:8:56][Nosleep]resuming, clear the timer
[2010-3-4 10:9:11][Nosleep]sleeping now, will add timer. next run time:2010-3-4 10:13:11
[2010-3-4 10:13:11][Nosleep]resuming, clear the timer
[2010-3-4 10:13:26][Nosleep]sleeping now, will add timer. next run time:2010-3-4 10:17:26
[2010-3-4 10:17:26][Nosleep]resuming, clear the timer
[2010-3-4 10:17:41][Nosleep]sleeping now, will add timer. next run time:2010-3-4 10:21:41
[2010-3-4 10:21:41][Nosleep]resuming, clear the timer
[2010-3-4 10:21:56][Nosleep]sleeping now, will add timer. next run time:2010-3-4 10:25:56
[2010-3-4 11:17:15][Nosleep]resuming, clear the timer
这里本来是应该10:25:56运行的,结果11:17:15才运行
[2010-3-4 11:18:14][Nosleep]sleeping now, will add timer. next run time:2010-3-4 11:22:14
[2010-3-4 11:20:2][Nosleep]resuming, clear the timer
[2010-3-4 11:26:42][Nosleep]sleeping now, will add timer. next run time:2010-3-4 11:30:42
[2010-3-4 11:30:42][Nosleep]resuming, clear the timer
[2010-3-4 11:30:57][Nosleep]sleeping now, will add timer. next run time:2010-3-4 11:34:57
[2010-3-4 11:48:34][Nosleep]resuming, clear the timer
这里本来是应该11:34:57运行的,结果11:48:34才运行
[2010-3-4 11:48:49][Nosleep]sleeping now, will add timer. next run time:2010-3-4 11:52:49


下面是代码:
void KeepPowerOn()
{
DWORD cbPowerMsgSize = PM_BROADCAST_QUEUE_SIZE;

MSGQUEUEOPTIONS options;
options.dwSize = sizeof(MSGQUEUEOPTIONS);
options.bReadAccess = TRUE;
options.cbMaxMessage = cbPowerMsgSize;
options.dwMaxMessages = 32;
options.dwFlags = MSGQUEUE_NOPRECOMMIT;

HANDLE hMsgQ = CreateMsgQueue(NULL,&options);

if(NULL == hMsgQ)
{
WriteMessage("[Nosleep]CreateMsgQueue Faild, Error: ",GetLastError());
return;
}

HANDLE hPowerNotify = ::RequestPowerNotifications(hMsgQ,PBT_TRANSITION);
if(NULL == hPowerNotify)
{
WriteMessage("[Nosleep]RequestPowerNotifications Faild, Error: ",GetLastError());
CloseMsgQueue(hMsgQ);

return;
}

union
{
WCHAR buf[PM_BROADCAST_QUEUE_SIZE];
POWER_BROADCAST powerBroadcast;
} u;


DWORD cbRead, dwFlags;
HANDLE hNotify = NULL;
while(TRUE)
{
WaitForSingleObject(hMsgQ,INFINITE);

if(!ReadMsgQueue(hMsgQ,&u,cbPowerMsgSize,&cbRead,0,&dwFlags))
{
WriteMessage("[Nosleep]ReadMsgQueue Faild, Error: ", GetLastError());
break;
}

POWER_BROADCAST ppb = u.powerBroadcast;

switch(ppb.Flags)
{
case 0x00400000:
{
union
{
FILETIME ft;
ULONGLONG ut;
}t;

SYSTEMTIME st;
GetLocalTime(&st);
DWORD dwSeconds=240;

SystemTimeToFileTime(&st, &t.ft);
t.ut+=UInt32x32To64(dwSeconds, 10000000);
FileTimeToSystemTime(&t.ft, &st);

char temp[100];
sprintf(temp,"[Nosleep]sleeping now, will add timer. next run time:%d-%d-%d %d:%d:%d\0",
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);

WriteMessage(temp);

CE_NOTIFICATION_TRIGGER cnt;
cnt.dwSize = sizeof(CE_NOTIFICATION_TRIGGER);
cnt.dwType = CNT_TIME;
cnt.dwEvent = NOTIFICATION_EVENT_NONE;
cnt.lpszApplication = TEXT("notexits.exe");
cnt.lpszArguments = TEXT("");

cnt.stEndTime = st;
cnt.stStartTime = st;

hNotify = CeSetUserNotificationEx(NULL,&cnt,NULL);
if(NULL == hNotify)
{
WriteMessage("[Nosleep]CeSetUserNotificationEx faild ", GetLastError());
}
}
break;
case 0x10000000:
{
WriteMessage("[Nosleep]resuming, clear the timer");
CeClearUserNotification(hNotify);
}
break;
default:
break;
}
}

if(hMsgQ)
::CloseMsgQueue(hMsgQ);
if(hPowerNotify)
::StopPowerNotifications(hPowerNotify);

WriteMessage("Exit now");
}

...全文
182 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iyond 2010-03-10
  • 打赏
  • 举报
回复
问题找到了。我是在系统将要进入休眠时调用CeSetUserNotificationEx,但有的时候,程序还没有执行到这句代码是,系统已经休眠了,所以设置失败。

有什么方法可以保证运行完我的代码在进入休眠吗?
yinjiaoo 2010-03-09
  • 打赏
  • 举报
回复
我的 在真机上面 指定的时候 可以运行。。
儿大不由爷 2010-03-05
  • 打赏
  • 举报
回复
我觉得还是case 0x00400000:
处理的问题,来的那个消息不定就是CeSetUserNotificationEx触发的,可能是系统其他部分触发的,检查一下CeSetUserNotificationEx调用的相关代码吧

7,659

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧