Need system clock advice.
-
Hello, I've written plenty of applications ran as scheduled tasks so I never had to worry about this before: When an application is being ran as windows service: What is the best way to test when the system clock hits a specific time (1:00am for example)? I know a timer is involved but is there a better way then just checking at an interval because you couldn't be sure that it would hit 1:00:00 exactly, you'd have to test a range of when it's greater then 1:00 and less then 1:01 kind of thing? Please let me know how you do it.... Thanks, Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous
-
Hello, I've written plenty of applications ran as scheduled tasks so I never had to worry about this before: When an application is being ran as windows service: What is the best way to test when the system clock hits a specific time (1:00am for example)? I know a timer is involved but is there a better way then just checking at an interval because you couldn't be sure that it would hit 1:00:00 exactly, you'd have to test a range of when it's greater then 1:00 and less then 1:01 kind of thing? Please let me know how you do it.... Thanks, Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous
This maybe absolute overkill for your application, but I still thought I'd point you to this: Quartz.NET[^]. It can be embedded into an application and has "tons" of useful features. Regards,
-
Hello, I've written plenty of applications ran as scheduled tasks so I never had to worry about this before: When an application is being ran as windows service: What is the best way to test when the system clock hits a specific time (1:00am for example)? I know a timer is involved but is there a better way then just checking at an interval because you couldn't be sure that it would hit 1:00:00 exactly, you'd have to test a range of when it's greater then 1:00 and less then 1:01 kind of thing? Please let me know how you do it.... Thanks, Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous
yep, you should treat timing values the same as floating-point values, i.e. avoid equality tests. If your action has to happen at a specific time T, and must be executed for sure, then IMO you should test for
T <= DateTime.Now
. If the action doesn't make sense any more when a span S has elapsed, then test for*T <= DateTime.Now) And (DateTime.Now < T.Add(S))
:)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
This maybe absolute overkill for your application, but I still thought I'd point you to this: Quartz.NET[^]. It can be embedded into an application and has "tons" of useful features. Regards,
-
yep, you should treat timing values the same as floating-point values, i.e. avoid equality tests. If your action has to happen at a specific time T, and must be executed for sure, then IMO you should test for
T <= DateTime.Now
. If the action doesn't make sense any more when a span S has elapsed, then test for*T <= DateTime.Now) And (DateTime.Now < T.Add(S))
:)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Thanks for the reply. Ya I would of done it something like that. Would you choose a timer or a thread.sleep loop?
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous
nlarson11 wrote:
Would you choose a timer or a thread.sleep loop?
Both are doable, and my choice depends on circumstances. Threads are more expensive than timers, yet they have automatic state: your thread waits where ever you make it wait, and it knows where it is, even if inside nested methods, loops, etc. Warning: with a single thread or timer, you can't have two actions running at the same time, which could be good or bad, depends on requirements. With a thread or timer per action (which could be expensive, depending on their number) actions could overlap and interfere with each other. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
nlarson11 wrote:
Would you choose a timer or a thread.sleep loop?
Both are doable, and my choice depends on circumstances. Threads are more expensive than timers, yet they have automatic state: your thread waits where ever you make it wait, and it knows where it is, even if inside nested methods, loops, etc. Warning: with a single thread or timer, you can't have two actions running at the same time, which could be good or bad, depends on requirements. With a thread or timer per action (which could be expensive, depending on their number) actions could overlap and interfere with each other. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Thanks for the input.
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
You're welcome. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hello, I've written plenty of applications ran as scheduled tasks so I never had to worry about this before: When an application is being ran as windows service: What is the best way to test when the system clock hits a specific time (1:00am for example)? I know a timer is involved but is there a better way then just checking at an interval because you couldn't be sure that it would hit 1:00:00 exactly, you'd have to test a range of when it's greater then 1:00 and less then 1:01 kind of thing? Please let me know how you do it.... Thanks, Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous
Get present time. Calculate period till specified time. Start a timer to "elapse" after that period. After completion of the job, start the timer again with the same logic.