By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,978 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,978 IT Pros & Developers. It's quick & easy.

Using sleep to delay execution for long periods of time?

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"Max" <ma*****@yahoo.com> schrieb
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

Nov 20 '05 #2

P: n/a
"Max" <ma*****@yahoo.com> schrieb
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

Nov 20 '05 #3

P: n/a
Max
Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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.
Nov 20 '05 #4

P: n/a
Max
Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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.
Nov 20 '05 #5

P: n/a
"Max" <ma*****@yahoo.com> schrieb

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.


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

Nov 20 '05 #6

P: n/a
"Max" <ma*****@yahoo.com> schrieb

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.


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

Nov 20 '05 #7

P: n/a
Max
Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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.


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?
Nov 20 '05 #8

P: n/a
Max
Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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.


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?
Nov 20 '05 #9

P: n/a
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/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/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" <ma*****@yahoo.com> wrote in message
news:kd********************@comcast.com...
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.

Nov 20 '05 #10

P: n/a
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/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/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" <ma*****@yahoo.com> wrote in message
news:kd********************@comcast.com...
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.

Nov 20 '05 #11

P: n/a
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" <ma*****@yahoo.com> wrote in message
news:YI********************@comcast.com... Armin Zingler wrote:
<<snip>>
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?

Nov 20 '05 #12

P: n/a
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" <ma*****@yahoo.com> wrote in message
news:YI********************@comcast.com... Armin Zingler wrote:
<<snip>>
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?

Nov 20 '05 #13

P: n/a
"Max" <ma*****@yahoo.com> schrieb
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

Nov 20 '05 #14

P: n/a
"Max" <ma*****@yahoo.com> schrieb
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

Nov 20 '05 #15

P: n/a
Max
Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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
Nov 20 '05 #16

P: n/a
Max
Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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
Nov 20 '05 #17

P: n/a
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

"Max" <ma*****@yahoo.com> wrote in message
news:nI********************@comcast.com... Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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

Nov 20 '05 #18

P: n/a
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

"Max" <ma*****@yahoo.com> wrote in message
news:nI********************@comcast.com... Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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

Nov 20 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.