PDA

View Full Version : qt4 daemon



akon
10th March 2009, 20:49
hello,
i need an application running background which will continously check current time to some predefine time, and emit a signal whenever match.

any help regarding this is a great favour.

wysota
10th March 2009, 21:23
Ok, but what exactly is the problem?

akon
10th March 2009, 21:39
i made a dbus interface, run a method where in a forever loop i tried ..



QDateTime aTime_=getTime(); //gives the same format as current
while(1){
QDateTime currTime=QDateTime::currentDateTime ();
qDebug()<<currTime << aTime_;
if(aTime_.date()==currTime.date()){
qDebug()<<"date checked"
emit signalTime();
}

//this part never calls
if(aTime_.time()==currTime.time()){
qDebug()<<"time checked";
emit signalTime();
}
}

aTime_.time()==currTime.time() ....this compare never calls, ???
another question is Is that the right way to make a daemon, or there is any other alternatives..

wysota
10th March 2009, 22:03
If you run a while(1) loop, you are starving the event loop and it's not possible for your application to do anything. You should use QTimer instead. And your comparison never works because you'd have to trigger it in the exact time the other object points to (up to miliseconds).

And have a look at this article:
http://doc.trolltech.com/qq/qq27-responsive-guis.html
Although it deals with GUI issues, it all applies to a non-gui application as well.

akon
10th March 2009, 22:09
thanks, much helpfull
i will check it out.

akon
11th March 2009, 07:51
hello wysota,
know its a FAQ...
may be I am doing some stupid mistake. it will be very helpful if u give a sample code snip, for this daemon running.

wysota
11th March 2009, 10:12
What exactly do you want? A snippet to use QTimer? Have you seen the docs of QTimer?

akon
11th March 2009, 11:35
i got the problem....but still not getting the solution...


QDateTime aTime_=getTime(); //gives the same format as current
QDateTime currTime;
currTime.setTimeSpec(Qt::UTC);
currTime=QDateTime::currentDateTime();

QTimer::singleShot(currTime.secsTo(aTime_)*1000, this, SLOT(slot_a_Mode()));

qDebug()<<currTime<<aTime_<< "seconds to call..."<<currTime.secsTo(aTime_);

____________

currTime.secsTo(aTime_); ----giving some unknown value, and I cant figure out why

wysota
11th March 2009, 13:07
Why are you compilating things so much?


QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(checkTime()));
timer->start(1000); // will fire every second

void ...::checkTime(){
QDateTime ct = QDateTime::currentDateTime();
if(qAbs(ct.toTime_t()-definedTime.toTime_t())<2){
emit alarm();
}
}

There is also another possibility, that you check the time before setting the timer and then set the timer to the exact number of miliseconds you need to reach the time you want.

akon
11th March 2009, 14:01
one thing :confused:, how u came to know I m trying something with alarm, :cool:

wysota
11th March 2009, 14:21
You said you wanted to signal the approach of a predefined moment in time. That's what we call raising an alarm :)