Using sleep to delay execution for long periods of time?

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

This is a follow-up on my previous thread concerning having the program
wait for a certain date and time and then executing some code when it
gets there. My question is; can I use the Sleep function from kernel32
to accomplish this? My concern is that this function takes milliseconds
where my program needs to be accurate to within 5 minuets, and delays
may be as long as a number of days, months or whatever. Would I run into
many problems doing it this way? I was thinking that if an overflow
occurs then I could just take the maximum value that Sleep would accept
and then once that is done having a function check if the time really
did come or if a further delay is needed. Please let me know what you
think of this or if there is a better way to do this.

One more thing, how would I be able to implement this so that the delay
of execution of tasks is completely separate from the rest of the
program? That is, the user should still be able to access all the forms,
change settings, and do all of that. I almost have a desire to write
something like a service, but don't have a clue of where to even start
on that.
 
Max said:
This is a follow-up on my previous thread concerning having the
program wait for a certain date and time and then executing some
code when it gets there. My question is; can I use the Sleep
function from kernel32 to accomplish this? My concern is that this
function takes milliseconds where my program needs to be accurate to
within 5 minuets, and delays may be as long as a number of days,
months or whatever. Would I run into many problems doing it this
way? I was thinking that if an overflow occurs then I could just
take the maximum value that Sleep would accept and then once that is
done having a function check if the time really did come or if a
further delay is needed. Please let me know what you think of this
or if there is a better way to do this.

Why not use a Timer and handle it's Elapsed event?
One more thing, how would I be able to implement this so that the
delay of execution of tasks is completely separate from the rest of
the program? That is, the user should still be able to access all
the forms, change settings, and do all of that. I almost have a
desire to write something like a service, but don't have a clue of
where to even start on that.

If you use the System.Timers.Timer, the Elapsed event occurs in a separate
thread.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Armin said:
Why not use a Timer and handle it's Elapsed event?




If you use the System.Timers.Timer, the Elapsed event occurs in a separate
thread.

How much CPU power will it take if I have the timer running for a number
of days? That's really my only concern, if it doesn't take almost any
resources and is accurate enough then I think that solves my problem.
 
Max said:
How much CPU power will it take if I have the timer running for a
number of days?

~ 0%, unless you want it to fire each millisecond, but as you said, much
less is sufficient.
That's really my only concern, if it doesn't take
almost any resources and is accurate enough then I think that solves
my problem.

CPU usage mainly depends on what you're doing when the timer event fires.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Armin said:
~ 0%, unless you want it to fire each millisecond, but as you said, much
less is sufficient.




CPU usage mainly depends on what you're doing when the timer event fires.

Ok I'm getting a hang of this and it seems to be working fine. Now
here's another question, in my program the minimum time between events
is 5 minuets. What do you think is better, to have the timer fire every
5 minuets and check if an event needs to be executed, or instead,
counting the number of years, months, days, hours, and minuets until the
next event, converting it into milliseconds, and setting that as the
interval? How accurate is this thing anyway? Would it even be able to
keep time for days and months or would I just be better of firing the
event every 5 minuets, synchronizing it with the system time, and then
starting it back up again?
 
Max,
In addition to the System.Timers.Timer you may want to consider using the
System.Threading.Timer class also

The Threading.Timer allows you to set a TimeSpan on when the first event
should occur. So if you need the event in 5 minutes you pass a TimeSpan of 5
minutes, if you need the first event in 3 days, you pass a TimeSpan of 3
days. If you need the first event in 1 day, 2 hour, 3 minutes and 44
seconds, then you pass a TimeSpan of 1 day, 2 hours, 3 minutes, and 44
seconds...

The Timers.Timer you would need to convert the above TimeSpan into a double
that represents the milliseconds (TimeSpan.TotalMilliseconds is good for
this).

Of course the Timers.Timer have an AutoReset feature if you need only a
single Elapsed event. The Threading.Timer has a similar ability...

Either way the following recent articles in MSDN Magazine explain the
difference between the three timer objects in .NET & when to use each.

http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx

http://msdn.microsoft.com/msdnmag/issues/04/03/default.aspx

Another thought is rather then rely on a Timer you may want to consider the
Task Scheduler under the Windows Control Panel.

Hope this helps
Jay
 
Max,
What do you think is better, to have the timer fire every
5 minuets and check if an event needs to be executed, or instead,
counting the number of years, months, days, hours, and minuets until the
I'm currently working on a Windows Service that I am using a Timers.Timer
that the default interval is 1 hour. When the Elapsed event is raised it
checks for any current, pass due & future work. If future work is less then
an hour away it shortens the interval so the next event occurs when the next
unit of work should occur. The Elapsed event will process any current & pass
due work...

I have not yet, but I will probably make the default 1 hour interval
configurable in the app.config file.

Hope this helps
Jay
 
Max said:
Ok I'm getting a hang of this and it seems to be working fine. Now
here's another question, in my program the minimum time between
events
is 5 minuets. What do you think is better, to have the timer fire
every 5 minuets and check if an event needs to be executed, or
instead, counting the number of years, months, days, hours, and
minuets until the next event, converting it into milliseconds, and
setting that as the interval? How accurate is this thing anyway?
Would it even be able to keep time for days and months or would I
just be better of firing the event every 5 minuets, synchronizing it
with the system time, and then starting it back up again?


I don't know how exact it is exactly, but if you use the first approach
you're on the safe side.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Armin said:
I don't know how exact it is exactly, but if you use the first approach
you're on the safe side.

All right guys, thanks for your help. Think I'll go with Jay’s method,
there's no need to sync every 5 mins so the timer will stop and sync
every hour unless an event needs to happen sooner. Below I've pasted the
code of a module that I came up with. So far it seems to work fine, but
I've only tested it up to an event in 5 minuets. All it does right now
is give you a msgbox with the current time so you can determine if it
happened when it should have. I am about to start the thing and see if
it is able to correctly give me a msgbox in 1 hour 5 minuets. If you
want to give it a try then simply call function "LoadTimer()" first and
then call "SetTimer()", just be sure to change the value of NextEvent.
Feel free to use it if you like:

Module mdlTime
Private timer As New Timers.Timer
Private time As Date
Private span As TimeSpan
Private Const NextEvent As String = "5/2/04 6:05:00 PM"

Public Sub LoadTimer()
'Call this function first in order to configure the timer
AddHandler timer.Elapsed, AddressOf TimerFired
timer.AutoReset = True
End Sub

Public Sub SetTimer()
Dim delay As Double

time = time.Parse(NextEvent)
span = time.Subtract(time.Now)

'See if next event occurs within the next hour,
'otherwise set the timer to wait till the next hour of the day
If span.Hours = 0 Then
delay = (span.Minutes * 60000) + (span.Seconds * 1000) + span.Milliseconds
Else
delay = 3600000 - time.Now.Minute * 60000 - time.Now.Second * 1000 -
time.Now.Millisecond
End If

timer.Interval = delay
timer.Start()

End Sub

Private Sub TimerFired(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)
timer.Stop()
MsgBox(time.Now)
SetTimer()
End Sub

End Module
 
Max,
You do realize that instead of:
delay = (span.Minutes * 60000) + (span.Seconds * 1000) + span.Milliseconds

You can use:
delay = span.TotalMilliseconds

Which will also take into account any Hours & Days in the TimeSpan.

Hope this helps
Jay
 
Back
Top