tags:

views:

627

answers:

2

My question is when the timer is start ticking.

Let's take for example, timer that configured to tick every second and I start my program on hh:mm:ss:ms = 22:40:34.50. The tick will start on round sec? Start on 17:40:35.00 or it's start on the exact time? So the first tick will be on 17:40:35.50 and the next one on 17:40:36.50.

What actually I'm trying to solve is I want to make function to work on 05:00:00.00 AM every day but only for one time. I've build a working hour system and sometimes workers forget to "close" their shift, so on 5am I want to check who didn't close, and make a error about it. How can you think I can solve issue?

+11  A: 

Windows is not real-time, meaning that things like "every second" are going to be approximations. I'm pretty sure that there isn't even a guarantee that, for example, a one-second timer will fire exactly 60 times per minute, or exactly 3600 times per hour.

If you need to do a specific task at 5:00 am, your best bet is to have a scheduled task that runs at 5:00 am, or a service that polls the time every few seconds and runs once 5:00 am passes.

Aric TenEyck
+1 for the scheduled task. It's amazing how many people want to program functionality that's built into the operating system
ChrisF
yes i can to that but i'm trying to reduce the programs that connecting to DB to make it more secure. it's just a simple functions that i want to run.. now it's working every 500ms, but mabye i can make it tick every sec and make sure it's always hits 5am...got it?
Mazki516
@Mazki516 - a scheduled task is the way to go. Windows will launch it at 5:00 am and it will only connect to the database when it's running.
ChrisF
Or, if it's really a concern, use a Job scheduled in your DB to avoid the extra application.
iammichael
@ChrisF - actually no, it may start at 5:00:01, too. Task Scheduler does not make guarantees on hard hits of the time either. Nothing in windows does.
TomTom
+3  A: 

The timer will start as soon as you call the Start method. It will not wait for a round second. The timer cannot guarantee exact times between ticks. If you give it an interval of 1 sec it will ensure that at least a second has passed since the last tick. You can solve your problem in a similar manner and perform your 5am work once a day the first time the timer elapses after 5am. Or if you feel confident that those who maintain the system will know about the scheduled tasks Aric TenEyck's suggestion of using a scheduled task is a great way to solve your issue.

RHicke