問題:如何在後臺定時器打勾?這是創建定時器線程的線程在時鐘滴答時仍然可以做其他事情。如何讓Windows定時器在後臺線程中運行
嘗試: - 使用_beginthreadex() - >它似乎有競爭條件
class Timer{
...
static unsigned __stdcall tick(void *param){
while(1){
Timer::timer++;
Sleep(Timer::timer*1000);
}
return 1;
}
}
.....
HANDLE time_thread = (HANDLE) _beginthreadex(0, 0, &Timer::tick,0,0,NULL);
...
//test for 20 seconds
//want to do something while the clock is not 20 seconds
//the mainthread here still has to receive input
//What is the proper way to do it?
while (Timer::getTime() != 20){
cout << Timer::getTime()
}
CloseHandle(time_thread);
...
注:使用IAM的Visual Studio 2008中,不是11,我沒有C++ 11的支持。
它會打印出像:00000001111111111112222222222222222333333333333333333 .....等等。你如何做到這一點,以便它在增加時,主線程仍然可以接收輸入?說的像。如果它不是20,你仍然可以打字 –
我們在談論一個控制檯程序或一個Windows GUI程序嗎? – mark
如果你只是想檢查控制檯輸入20秒,然後繼續,如果你沒有任何,你應該看'GetStdHandle(STD_INPUT_HANDLE)'和'WaitForSingleObject',並跳過線程。您可能還需要根據您嘗試執行的操作來設置「SetConsoleMode」。 – mark