473,387 Members | 1,388 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,387 software developers and data experts.

Timing Question


Hey every body i had a question

i am in process of writing an application, where this application needs
to check the database on hourly bases to see if they are any information
that are needed to be processed in the next upcoming hour
so my application has to to connect to data base right on (for example)
5:00:00 then check all the jobs that are due on
5:00:00
5:15:00
5:30:00
5:45:00

now i have no problem connecting to database and getting the list of
jobs that are due on that upcoming hour...my question is in timing

i want my application to fire a method called (CheckForNewBatches) on
each hour, which it does,,,but if my application starts on for example
4:30:27 i want it to wait till next hour which would 5:00:00 (excatly
other wise sql wouldnot return any thing) so even seconds should be
zero. so on new Timer method i have to give it the number of
milliseconds to wait to next hour..but i always get a wrong hour..for
example if i start my application at 4:30 the method wouldnot fire till
5:00:58 which is not what i want ..i want it to be right on
5:00:00....can some one tell what they see is wrong with my code

so in my consturctor i wrote something like that

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
Timer(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);
=====EndofConstructor
// Should return the number of milliseconds to wait untill next hour
internal int MilliSecondsToDelay {
get {

if (startTime == DateTime.MinValue)
startTime = DateTime.Now;
if (startTime.Minute != 0)
return (3600000 - (startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month, startTime.Day, startTime.Hour, 0, 0);
return 0;
}
}
}
private void CheckForNewBatches(Object sender){
///Do whatever
}
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
9 1725
Amir,

Instead of having your application perform this timing, why not just set
it up as a scheduled task on the machine? Have your application perform
only the logic to perform the tasks you need it to do (not the timing
logic), and then have the scheduled task manager run the task ever hour.
This way, you don't have to worry about it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

Hey every body i had a question

i am in process of writing an application, where this application needs
to check the database on hourly bases to see if they are any information
that are needed to be processed in the next upcoming hour
so my application has to to connect to data base right on (for example)
5:00:00 then check all the jobs that are due on
5:00:00
5:15:00
5:30:00
5:45:00

now i have no problem connecting to database and getting the list of
jobs that are due on that upcoming hour...my question is in timing

i want my application to fire a method called (CheckForNewBatches) on
each hour, which it does,,,but if my application starts on for example
4:30:27 i want it to wait till next hour which would 5:00:00 (excatly
other wise sql wouldnot return any thing) so even seconds should be
zero. so on new Timer method i have to give it the number of
milliseconds to wait to next hour..but i always get a wrong hour..for
example if i start my application at 4:30 the method wouldnot fire till
5:00:58 which is not what i want ..i want it to be right on
5:00:00....can some one tell what they see is wrong with my code

so in my consturctor i wrote something like that

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
Timer(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);
=====EndofConstructor
// Should return the number of milliseconds to wait untill next hour
internal int MilliSecondsToDelay {
get {

if (startTime == DateTime.MinValue)
startTime = DateTime.Now;
if (startTime.Minute != 0)
return (3600000 - (startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month, startTime.Day, startTime.Hour, 0, 0);
return 0;
}
}
}
private void CheckForNewBatches(Object sender){
///Do whatever
}
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
Hi,
There is nothing wrong with your code. and there is nothing you can do to
solve it :)

You cannot be sure that your app will start running at EXACTLY 5:00:00 , at
that moment an event is send and processed later on, so it's normal it will
take a little longer to wait.

A similar thing if you follow Paldino's suggestions, in short WinXX is not a
real time OS.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

Hey every body i had a question

i am in process of writing an application, where this application needs
to check the database on hourly bases to see if they are any information
that are needed to be processed in the next upcoming hour
so my application has to to connect to data base right on (for example)
5:00:00 then check all the jobs that are due on
5:00:00
5:15:00
5:30:00
5:45:00

now i have no problem connecting to database and getting the list of
jobs that are due on that upcoming hour...my question is in timing

i want my application to fire a method called (CheckForNewBatches) on
each hour, which it does,,,but if my application starts on for example
4:30:27 i want it to wait till next hour which would 5:00:00 (excatly
other wise sql wouldnot return any thing) so even seconds should be
zero. so on new Timer method i have to give it the number of
milliseconds to wait to next hour..but i always get a wrong hour..for
example if i start my application at 4:30 the method wouldnot fire till
5:00:58 which is not what i want ..i want it to be right on
5:00:00....can some one tell what they see is wrong with my code

so in my consturctor i wrote something like that

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
Timer(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);
=====EndofConstructor
// Should return the number of milliseconds to wait untill next hour
internal int MilliSecondsToDelay {
get {

if (startTime == DateTime.MinValue)
startTime = DateTime.Now;
if (startTime.Minute != 0)
return (3600000 - (startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month, startTime.Day, startTime.Hour, 0, 0);
return 0;
}
}
}
private void CheckForNewBatches(Object sender){
///Do whatever
}
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #3
hi thanks for your replies guys

but i canot use the schdeule taks due to the fact that this exe has to
be running all the time ...it does alot of things like managing a
webapplication Database, if i were to use windows schedule task manager
i have to create a new instance of the application every hour...which i
dont really want to do ..it would desotry my whole logic plus... windows
would run out of memory :P ....any other suggestions?
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4
Amir,

There are really no other suggestions. You will have to calculate the
amount of time to wait from the time you set the timer property until the
next time you have to run a task.

Also, I don't see why your machine would run out of memory if you used a
task scheduler. Once the executable is done, the process disappears, and
memory is reclaimed by the OS.

If anything, you run a greater risk of running out of memory by having
your own continuous process run. If you are doing nothing but running tasks
on a timed basis, then the scheduled task manager is really the way to go.
No need to reinvent the wheel. Also, as Ignacio said, this is not a
real-time OS, so you won't be able to get it to execute EXACTLY at 5 PM.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message
news:OM******************@TK2MSFTNGP14.phx.gbl...
hi thanks for your replies guys

but i canot use the schdeule taks due to the fact that this exe has to
be running all the time ...it does alot of things like managing a
webapplication Database, if i were to use windows schedule task manager
i have to create a new instance of the application every hour...which i
dont really want to do ..it would desotry my whole logic plus... windows
would run out of memory :P ....any other suggestions?
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #5
Tell me why would sql not return anything when you run the query at 5:00:01
for instance?

Willy.

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

Hey every body i had a question

i am in process of writing an application, where this application needs
to check the database on hourly bases to see if they are any information
that are needed to be processed in the next upcoming hour
so my application has to to connect to data base right on (for example)
5:00:00 then check all the jobs that are due on
5:00:00
5:15:00
5:30:00
5:45:00

now i have no problem connecting to database and getting the list of
jobs that are due on that upcoming hour...my question is in timing

i want my application to fire a method called (CheckForNewBatches) on
each hour, which it does,,,but if my application starts on for example
4:30:27 i want it to wait till next hour which would 5:00:00 (excatly
other wise sql wouldnot return any thing) so even seconds should be
zero. so on new Timer method i have to give it the number of
milliseconds to wait to next hour..but i always get a wrong hour..for
example if i start my application at 4:30 the method wouldnot fire till
5:00:58 which is not what i want ..i want it to be right on
5:00:00....can some one tell what they see is wrong with my code

so in my consturctor i wrote something like that

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
Timer(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);
=====EndofConstructor
// Should return the number of milliseconds to wait untill next hour
internal int MilliSecondsToDelay {
get {

if (startTime == DateTime.MinValue)
startTime = DateTime.Now;
if (startTime.Minute != 0)
return (3600000 - (startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month, startTime.Day, startTime.Hour, 0, 0);
return 0;
}
}
}
private void CheckForNewBatches(Object sender){
///Do whatever
}
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #6
Hi,

Then you are stuck with a window service, and accept that you will not get
executed at an exxact time, not even every 1 hour sharp, there will always
be a lag, depending of how busy your computer is.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message
news:OM******************@TK2MSFTNGP14.phx.gbl...
hi thanks for your replies guys

but i canot use the schdeule taks due to the fact that this exe has to
be running all the time ...it does alot of things like managing a
webapplication Database, if i were to use windows schedule task manager
i have to create a new instance of the application every hour...which i
dont really want to do ..it would desotry my whole logic plus... windows
would run out of memory :P ....any other suggestions?
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #7
ok guys thank you for all yoru responses

actually this gets me the excat time to the secnod
return ((59 - DateTime.Now.Minute) * 60 * 1000) + ((60 -
DateTime.Now.Second) * 1000);

thank you again

oh btw about that guy who asked why sql wouldnot return any thing is
becuase users only are allowed to set atime in formats of

5:00
5:15:00
5:30:00
5:45:00

so the seconds inside my sql table are always zero
if my time doesnot have a zero seconds. then my select statment wouldnot
return any thing

any way thanks again

---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #8
That's a load of garbage Amir.

If you construct your SQL 'select' statement correctly it will pickup
anything you tell it too.

E.G.: Run at 5:00 AM or as soon as possible thereafter:

"select <select_list> from <table_name> where <date_column_name> >= '" +
DateTime.Now.ToString("HH") + ":00' and " +
"<date_column_name> < '" + DateTime.Now.AddHour(1).ToString("HH") + ":00"

In the real world, the planet keeps turning even if a task that is supposed
to be started on the hour is not executed until a short time after the hour.
I have yet to see someone get fired because they were a second late in
starting a task.

When I need such finctionality, I tend to use a thread that iterates and at
the end of each iteration sleeps for, say, 100 milliseconds. Each time it
wakes I check to see if it needs to do something.

In this case the task would get started approxiamately 1 tenth of a second
after the hour and think that something similar would suit you purpose
admirably.

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message
news:e2**************@TK2MSFTNGP09.phx.gbl...
ok guys thank you for all yoru responses

actually this gets me the excat time to the secnod
return ((59 - DateTime.Now.Minute) * 60 * 1000) + ((60 -
DateTime.Now.Second) * 1000);

thank you again

oh btw about that guy who asked why sql wouldnot return any thing is
becuase users only are allowed to set atime in formats of

5:00
5:15:00
5:30:00
5:45:00

so the seconds inside my sql table are always zero
if my time doesnot have a zero seconds. then my select statment wouldnot
return any thing

any way thanks again

---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #9
I would want to believe that you can manipulate the query to get the data that's stored in the database anytime you want.

In case you are not, why don't you setup the timer to fire the callback 5 mins before the actual time you wanted it to fire and then wait for the correct time to fire the query, this way you have a good chance of firing the query exactly at the time you want, maybe a couple of seconds here and there.

And use the Timer type implemented in the System.Threading namespace for a more accurate timer implementation.

HTH, Metallikanz!

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message news:%2***************@TK2MSFTNGP10.phx.gbl...

Hey every body i had a question

i am in process of writing an application, where this application needs
to check the database on hourly bases to see if they are any information
that are needed to be processed in the next upcoming hour
so my application has to to connect to data base right on (for example)
5:00:00 then check all the jobs that are due on
5:00:00
5:15:00
5:30:00
5:45:00

now i have no problem connecting to database and getting the list of
jobs that are due on that upcoming hour...my question is in timing

i want my application to fire a method called (CheckForNewBatches) on
each hour, which it does,,,but if my application starts on for example
4:30:27 i want it to wait till next hour which would 5:00:00 (excatly
other wise sql wouldnot return any thing) so even seconds should be
zero. so on new Timer method i have to give it the number of
milliseconds to wait to next hour..but i always get a wrong hour..for
example if i start my application at 4:30 the method wouldnot fire till
5:00:58 which is not what i want ..i want it to be right on
5:00:00....can some one tell what they see is wrong with my code

so in my consturctor i wrote something like that

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
Timer(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);


=====EndofConstructor


// Should return the number of milliseconds to wait untill next hour
internal int MilliSecondsToDelay {
get {

if (startTime == DateTime.MinValue)
startTime = DateTime.Now;


if (startTime.Minute != 0)
return (3600000 - (startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month, startTime.Day, startTime.Hour, 0, 0);
return 0;
}
}
}


private void CheckForNewBatches(Object sender){
///Do whatever
}
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #10

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

Similar topics

6
by: S. David Rose | last post by:
Hello All! I am new to Python, and wanted to know if I might ask you a question regarding timing. I want a main loop which takes photos from 8 different web-cams, each can be addressed by...
10
by: Greg Stark | last post by:
This query is odd, it seems to be taking over a second according to my log_duration logs and according to psql's \timing numbers. However explain analyze says it's running in about a third of a...
7
by: jamie | last post by:
hey all, I am attempting to do motion control for a final project, but I have a concern.... For motion control, timing is everyting, the better it is, the better it works. Currently I am...
1
by: AVance | last post by:
Hi, I've come across this scenario in ASP.NET 1.1 with forms authentication where the forms auth doesn't seem to timeout correctly, nor redirect to the login page. I have done some testing, and...
1
by: Novice | last post by:
Hi all, I'm at my wit's end on trying to insert some timing code into the server side code that parses the hashed data contained in the hidden field being submitted to the server I've tried...
3
by: gregory_may | last post by:
I have an application where I am using a System Thread to capture the screen & Broadcast it to clients. Its "working", but the timing on the background thread gets wildly erratic at times. Some...
2
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
4
by: Thomas R. Hummel | last post by:
We are using SQL Server 2005. The SP in question is a fairly simple select statement. When the developers run their unit tests it is timing out (15 second timeout is set from their end). When I run...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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...

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.