Reputation: 165
Here's some runnable code, that should take 30 seconds to execute. My computer does it in about 2, regardless of the QuadPart value. What am I doing wrong? I'm just trying to use a reliable timer so I can have a constant frame rate, so I'm open to using something else if it is easier / more reliable.
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;
}
Thanks.
Upvotes: 0
Views: 264
Reputation: 2489
liDueTime.QuadPart = -10,000LL * period ;
Are you using a comma operator here or is it just a typo? Remove the comma.
Upvotes: 1