不使用Sleep,如何让线程没任务时不占用CPU资源

//=========================================================================
         // 函数名称: SuperSleep
         // 函数?明: void  高精度延时,cpu占用低,窗口不卡死,一次最大可延时几年 
         //=========================================================================
         // 参    数: __in int nTime            1000微秒 = 1毫秒 ; 1000毫秒 = 1秒
         // 参    数: __in int nSpeed        可空:毫秒  0 毫秒  1 微秒  2 秒  3 分  4 小时  5 天
         //=========================================================================
         static  void  SuperSleep(__in  int  nTime,__in  int  nSpeed = 0);
 
         //=========================================================================
         // 函数名称: ProcessEvent
         // 函数?明: void 暂时转让控制权,以便让 Windows 操作系统有机会处理其它的如用户键盘或鼠标输入等事件。
         //                 直到操作系统处理并发送完程序队列中的所有事件后,命令才会返回。
         //=========================================================================
         static  void  ProcessEvent();
 
void  CLibX::System::SuperSleep( __in  int  nTime,__in  int  nSpeed  /*= 0*/  )
{
     LARGE_INTEGER lar;
     if  (nSpeed == 0) nSpeed = 1;
     else  if  (nSpeed == 1) nSpeed = -10;
     else  if  (nSpeed == 2) nSpeed = 1000;
     else  if  (nSpeed == 3) nSpeed = 1000 * 60; 
     else  if  (nSpeed == 4) nSpeed = 1000 * 60 * 60; 
     else  if  (nSpeed == 5) nSpeed = 1000 * 60 * 60 * 24; 
 
     lar.QuadPart = -10 * nTime * nSpeed * 1000 ;
     HANDLE  hTimer = CreateWaitableTimer(NULL,FALSE,NULL);
     SetWaitableTimer(hTimer,&lar,NULL,NULL,NULL,FALSE);
     while  (MsgWaitForMultipleObjects(1,&hTimer,FALSE,INFINITE,QS_ALLINPUT) != WAIT_OBJECT_0)
     {
         CLibX::System::ProcessEvent();
     }
 
     CloseHandle(hTimer);
}
 
void  CLibX::System::ProcessEvent()
{
     MSG msg;
     while (PeekMessage(&msg,NULL,NULL,NULL,1) != 0)
     {
         DispatchMessage(&msg);
         TranslateMessage(&msg);
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值