首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

可等候定时器

2012-11-04 
可等待定时器1. 代码中的for循环的 那个 sleepex表示什么意思?其参数第一个是-1, 第二个是 truemsdn:第一

可等待定时器
1. 代码中的for循环的 那个 sleepex表示什么意思?

其参数第一个是-1, 第二个是 true

 msdn:

第一个参数:-1表示 挂起的不会超时

第二个参数为false.表示函数不会返回,只有在时间爱你周期消失后才会返回。

如果apc函数投递到线程的apc队列中,函数则不会返回,并且apc 函数也不会执行的。

如果是true,则对于apc函数,时间周期一旦消失,就会被调用。



这是我翻译的, 没怎么看懂,谁能够结合着代码,解释一下这2个参数!!!





2.可等待定时器什么情况下使用?平时见的很少。


3.可等待定时器涉及到一个概念:apc函数。 窗口回调函数,是不是属于apc?

线程函数属于嘛?




C/C++ code
#define _WIN32_WINNT 0x0500#include < windows.h >#include < stdio.h > #define _SECOND 10000000typedef struct _MYDATA {   TCHAR *szText;   DWORD dwValue;} MYDATA;VOID CALLBACK TimerAPCProc(   LPVOID lpArg,               // Data value   DWORD dwTimerLowValue,      // Timer low value   DWORD dwTimerHighValue )    // Timer high value{   MYDATA *pMyData = (MYDATA *)lpArg;   printf( "Message: %s\nValue: %d\n\n", pMyData->szText,          pMyData->dwValue );   MessageBeep(0);}void main( void ) {   HANDLE          hTimer;   BOOL            bSuccess;   __int64         qwDueTime;   LARGE_INTEGER   liDueTime;   MYDATA          MyData;   TCHAR           szError[255];   MyData.szText = "This is my data.";   MyData.dwValue = 100;   if ( hTimer = CreateWaitableTimer(           NULL,                   // Default security attributes           FALSE,                  // Create auto-reset timer           "MyTimer" ) )           // Name of waitable timer   {      __try       {         // Create an integer that will be used to signal the timer          // 5 seconds from now.         qwDueTime = -5 * _SECOND;         // Copy the relative time into a LARGE_INTEGER.         liDueTime.LowPart  = (DWORD) ( qwDueTime & 0xFFFFFFFF );         liDueTime.HighPart = (LONG)  ( qwDueTime >> 32 );         bSuccess = SetWaitableTimer(            hTimer,           // Handle to the timer object            &liDueTime,       // When timer will become signaled            2000,             // Periodic timer interval of 2 seconds            TimerAPCProc,     // Completion routine            &MyData,          // Argument to the completion routine            FALSE );          // Do not restore a suspended system         if ( bSuccess )          {            for ( ; MyData.dwValue < 1000; MyData.dwValue += 100 )             {               SleepEx(                  INFINITE,     // Wait forever                  TRUE );       // Put thread in an alertable state            }         }          else          {            wsprintf( szError, "SetWaitableTimer failed with Error \               %d.", GetLastError() );            MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );         }      }       __finally       {         CloseHandle( hTimer );      }   }    else    {      wsprintf( szError, "CreateWaitableTimer failed with Error %d.",           GetLastError() );      MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );   }}


[解决办法]
探讨

你好, 你能帮吗解答

主贴子里的

for()
{

sleepex

}

这段代码嘛?


是什么意思呢?

其中时间是-1,为什么如此设置。

实在不懂

热点排行