0
以下是一些可運行的代碼,需要30秒才能執行。 無論QuadPart的值是多少,我的電腦都會在2個左右。 我在做什麼錯?我只是想用一個可靠的定時器,所以我可以有一個固定的幀速率,所以如果它更容易/更可靠,我願意使用其他的東西。Windows WaitableTimers - 找不到錯誤
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;
}
謝謝。
就是這樣。我不能相信我這麼做......正在計算一些地方來弄清楚這個數字有多大。逗號操作符在這樣的數字上做了什麼? – Grommit
嗯......用這種方法使用逗號運算符可以將值「-10」賦值給'QuadPart',然後長八進制零('000LL')乘以'period',丟棄乘法結果。 –
@Grommit順便說一句,如果真的是這樣,你會把我的答案標記爲接受嗎? –