473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Using sleep to delay execution for long periods of time?

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
18 3222
"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
"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
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
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
"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
"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
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
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
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
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
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
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
"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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ryan Spencer | last post by:
Hello Everyone, I want to have a row of periods, separated by small, say, .5 second intervals between each other. Thus, for example, making it have the appearance of a progress "bar". ...
4
by: Xaradas | last post by:
How can I suspend the execution of a script for x seconds? Does exists something like sleep(x)? Thanks. ------------------------------------ "Computer Science is no more about computers ...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
11
by: Philip Parker | last post by:
any ideas? sleep(1) is far too long a delay, im looking for something in the range of 50-100 milliseconds at most. something which doesnt suck up cpu time too much would be nice. is there anything...
5
by: Erich Schreiber | last post by:
In the Python Library Reference the explanation of the time.sleep() function reads amongst others: > The actual suspension time may be less than that requested because > any caught signal will...
8
by: John | last post by:
Hi all: Is there a C function to make a procedure sleep or delay for a few seconds/minutes on Linux and Sun OS platform? Thanks
0
by: Max | last post by:
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...
2
by: jrsjrs | last post by:
<?php echo "<BR><BR>A) Present time is:<BR>"; echo date('h:i:s') . "\n<BR>Delaying 5 seconds, new time is:<BR>"; sleep(5); echo date('h:i:s') . "\n<BR>"; ?> This first script delays 5...
2
by: virupax | last post by:
Which is the best one to use sleep or delay ( for a delay in the process ) .As i understand use of sleep, allows the kernel to schedule the process. And if a sleep of microseconds is used, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.