Timing Question

  • Thread starter Thread starter Amir Ghezelbash
  • Start date Start date
A

Amir Ghezelbash

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,3600000);


=====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
}
 
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.
 
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,
 
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?
 
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.
 
Tell me why would sql not return anything when you run the query at 5:00:01
for instance?

Willy.
 
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,
 
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
 
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.
 
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top