473,545 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3238
"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.T imer, 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.T imer, 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.T imer, 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.T imer, 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.T imer, 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.T imer, 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.T imer, 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.T imer, 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.T imer you may want to consider using the
System.Threadin g.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.Total Milliseconds 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******** ************@co mcast.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5680
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". import time sleep(.5)
4
68007
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 than astronomy is about telescopes."
21
18678
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 nanosleep(), depending on platform }
11
1775
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 in the standard cpp library which can do this? another thing. how can i make a static class function member , access things in an object of the...
5
9075
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 terminate the sleep() following execution > of that signal's catching routine. Also, the suspension time may > be longer than requested by an...
8
14745
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
374
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 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,...
2
1525
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 seconds then prints everything. How can we make it immediately print the present time, tell us that there is a delay,and then print
2
6141
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 the kernel schedules at this time, will it takes care to reschedule the interrupted process, back in the requested amount of sleep the process had...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.