0

这是一些可运行的代码,应该需要 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;
}

谢谢。

4

1 回答 1

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

您在这里使用逗号运算符还是只是一个错字?去掉逗号。

于 2013-05-28T17:43:23.303 回答