PDA

View Full Version : How to make scheduler in QT4.4.2?



merry
14th November 2008, 11:16
Hi all

Working on Qt4.4.2 on my Intel-mac machine, I want to develop a Time Scheduler in Qt, So that my application runs automatically acc. to the scheduled time.

calhal
14th November 2008, 11:45
Use QDateTime class. Make a list with tasks and dates and compare the dates periodically with QDateTime::currentDateTime().

stevey
31st August 2009, 08:12
I've done this in an application of mine, using QTimer polling every 5 - 10 seconds or so.
The only problem with the technique I find though is that if the tick happens 1 second before the start time, then it starts 4 seconds late.
Apart from actually shortening the interval, is there another way to get the schedule launched bang on time?

I understand that win32 has events you can hook into an event and wait using WaitForSingleObject which fires as soon as the clock ticks to the start time you're interested in. I'm not sure how crontab does it under *nix, but I've never seen a process start late before.
I'm feeling that it's going to be platform specific code here to get a reliable scheduler going. Am I wrong?


Steve

wysota
31st August 2009, 10:24
Apart from actually shortening the interval, is there another way to get the schedule launched bang on time?

You don't have to periodically check the queue. You know when the first event in the queue will occur so you can set the timer to exactly that moment in time. If someone happens to add another event which is earlier than the one that used to be the first one, simply adjust the timer.

stevey
31st August 2009, 12:22
Sorry Wysota, which "queue" are you talking about here?
Are you relating to "Single Shot" or something else?
Any chance of a code sample to fire at 10:00am?

wysota
31st August 2009, 12:31
QTimer timer;
timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), ..., SLOT(processQueue()));
QList<Task> tasks;
tasks.insert(...);
QDateTime now = QDateTime::currentDateTime();
QDateTime expected = tasks.top().dateTime(); // i.e. 09-08-31 10:00
uint secs = expected.toTime_t() - now.toTime_t();
timer.stop();
timer.start(secs*1000);
//...

void ...::processQueue(){
Task t = queue.first();
queue.removeFirst();
t.perform();
if(queue.isEmpty()) return;
QDateTime now = QDateTime::currentDateTime();
QDateTime expected = tasks.top().dateTime();
uint secs = expected.toTime_t() - now.toTime_t();
timer.stop();
timer.start(secs*1000);
}