How to create delay
I need to create a delay timer without using Sleep()
from windows.h
since it will pause current and child threads. So i need a simple delay timer to apply int开发者_运维知识库o my program.
Thank you.
Polling is a bad idea since it means a busy loop. The normal way to do this on Windows is with a timer. See SetTimer()
.
Sleep
does not affect any other threads. If you find that all your threads are stopped when you call Sleep
, it means that the sleeping thread has a resource that they are all waiting on. Find out what that resource is, otherwise your program can't possibly work as planned.
You can just make a loop and poll the system timer repeatedly. This causes full CPU usage, however - using Sleep
(either directly or indirectly) is how delays are usually implemented.
Alternatively, you can create an event and call WaitForSingleObject
with the desired delay. But that's crazy, Sleep
really does the same thing but better.
You need to use some loop and count the seconds at the same time you have to keep up the message pumping too. This will make ur program responsive.
Why delay? delay is not really good idea. You should think of using synchronization object like mutex, event.
精彩评论