问题:如何在后台定时器打勾?这是创建定时器线程的线程在时钟滴答时仍然可以做其他事情。如何让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