Windows WaitableTimers - 找不到错误

时间:2013-05-28 17:38:56

标签: c++ windows timer

这是一些可运行的代码,需要30秒才能执行。 无论QuadPart值如何,我的计算机都会在2左右完成。 我究竟做错了什么?我只是想使用一个可靠的计时器,所以我可以有一个恒定的帧率,所以如果它更容易/更可靠,我愿意使用别的东西。

int _tmain(int argc, _TCHAR* argv[])
{

    cout << "enter the the period, in mS" << endl;

    unsigned int period;

    cin >> period;


    HANDLE hTimer = NULL;
    LARGE_INTEGER liDueTime;

    liDueTime.QuadPart = -10,000LL * period  ; // Units are 100 ns, so 10,000 is 1mS, negative makes it relative time.

    // Create an unnamed waitable timer.
    hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
    if (NULL == hTimer)
    {
        cout << "CreateWaitableTimer failed " << GetLastError() << endl;
        // return 1;
    }

    float Hz = 1000.0f/period; // 1000 converts from mS to S. 

    int cycles = Hz  * 30.0f; // run for 30 seconds.

    cout << "This should take 30 seconds, there are " << cycles << " cycles to do" << endl;

    for (int i=0; i<cycles; i++)
    {

        if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0))
        {
            cout << "SetWaitableTimer failed " << GetLastError() << endl;
            //  return 2;
        }

        // do the work
        someTask(); // you can comment this out, I just counted to 10000


        // now just wait.
        if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
            cout << "WaitForSingleObject failed " << GetLastError();
        else 
        {
            // printf("Timer was signaled.\n");
        }
    }

cout << "Done" << endl;
return 0;
}

感谢。

1 个答案:

答案 0 :(得分:1)

liDueTime.QuadPart = -10,000LL * period  ;

你在这里使用逗号运算符还是只是一个错字?删除逗号。