Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all,

I want to make a timer to schedule a task. I intend make a loop forever and get time by GetLocalTime as:

C++
while(1)
{
   LPSYSTEMTIME time;
   GetLocalTime(&time);
   if(time.wHour == 10)
   {
       //do some tasks
   }
   
}


However, I realize above code will make CPU always busy because of while(1). So there is any method to schedule to carry out specified task without "while(1)" to check. For ex: Interrupt, when local time is 10h, there is an interrupt to wake up my code?

Thank your for you comments
Posted
Updated 26-Nov-11 16:02pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Nov-11 23:01pm    
Why not just using one of system timers? You cannot make your own anyway...
--SA

1 solution

You cannot make a true timer without using timer features of OS. A timer is always an OS feature. It uses hardware and a device driver to trigger some event/message at desired time using zero CPU time while waiting for a timer event. Application-level code does not get direct access to hardware and hardware interrupts, so no matter what you do, it's useless.

In Windows, there is a number of different timers you can use. For further information, please see this CodeProject article: Timers Tutorial[^].

—SA
 
Share this answer
 
Comments
SVPro 26-Nov-11 23:09pm    
Thanks @SAKryukov about useful information, I will investigate about Timer later
Sergey Alexandrovich Kryukov 26-Nov-11 23:20pm    
You are welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900