PDA

View Full Version : Multiple timers in single Application



Qt Coder
25th March 2009, 11:01
Hello all,


I m using Qt 4.4.3


Can we add multiple timers in single Qt application.

I done as following


QTimer *timer;
QTimer *timerEllipse;

timer = new QTimer(this);
timerEllipse = new QTimer(this);

connect(timer, SIGNAL(timeout()), this, SLOT(RotateBG()));
connect(timerEllipse, SIGNAL(timeout()), this, SLOT(ChangeEllipse()));

RotateBG()
{
//Code to Rotate background
}

ChangeEllipse()
{
//Code to change color of ellipse
}


But only first timer function is executed every time ... why is it so??

^NyAw^
25th March 2009, 11:08
Hi,

Where do you set timeout interval of timers and where do you start the timers?
Is "ChangeEllipse" defined as slot? What return value are you getting on connection statment(use a boolean as return value) and the is there any error message on console output?

Qt Coder
25th March 2009, 11:21
:)


The problem was that I forgot to add "ChangeEllipse" as slot and added as normal function in class.


I changed it and now its working

Thank you very much!!!!!!!

mahi6a1985
18th September 2012, 13:28
Hi ^NyAw^,

I am having the same problem. Can u help me plzz..
As my application is having 6 timers with different intervals. The delay is occurring for updating the fields in the app.

if i ran only 2 timers the updating speed is less than 80ms.

if i run all 6 timers then updating speed of app is almost 4 secs.

Santosh Reddy
18th September 2012, 13:56
The OP problem is sovled, if your problem is same then you already have a solution.

Does all your timers complete in time ?

t1+t2+t3+t4+t5+t6+system response time = response time (which is in your case is 4 seconds)
tx = time taken by corresponding timer slot to execute

t1+t2+t3+t4+t5+t6 should be less than the minimum timer value among all the 6 timer values

mahi6a1985
20th September 2012, 11:14
The OP problem is sovled, if your problem is same then you already have a solution.

Does all your timers complete in time ?

t1+t2+t3+t4+t5+t6+system response time = response time (which is in your case is 4 seconds)
tx = time taken by corresponding timer slot to execute

t1+t2+t3+t4+t5+t6 should be less than the minimum timer value among all the 6 timer values


Hi,
Here is what my problem indetail

I am newbie to QT. As i am developing an application, got struck with the timers controls. I want to run 6 timers at time in some conditions. and in some conditions 3 timers.

In this process 2 timers got delayed bcoz of the other as per my knowledge i think so.

My timers n its intervals r as follows -

HI all,

I am newbie to QT. As i am developing an application, got struck with the timers controls. I want to run 6 timers at time in some conditions. and in some conditions 3 timers.

In this process 2 timers got delayed bcoz of the other as per my knowledge i think so.

My timers n its intervals r as follows -


1st timer -
timer_HMIScrLvl4 = new QTimer(this);
connect(timer_HMIScrLvl4, SIGNAL(timeout()), this, SLOT(update_HMIScrLvl4()));
timer_HMIScrLvl4->setInterval(100);
2nd timer -
Timer_failure_display = new QTimer(this);
connect(Timer_failure_display, SIGNAL(timeout()), this, SLOT(failure_display()));
Timer_failure_display->setInterval(250);
3rd timer -
Timer_footer = new QTimer(this);
connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
Timer_footer->setInterval(250);
4th timer -
Timer_diagonostic_refresh = new QTimer(this);
connect(Timer_diagonostic_refresh, SIGNAL(timeout()), this, SLOT(diagonostic_refresh()));
Timer_diagonostic_refresh->setInterval(50);
5th Timer -
Timer_information_refresh = new QTimer(this);
connect(Timer_information_refresh, SIGNAL(timeout()), this, SLOT(information_refresh()));
Timer_information_refresh->setInterval(25);
6th timer -
Timer_footer = new QTimer(this);
connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
Timer_footer->setInterval(250);


1st & 2nd timers are the main timers i should not wanted them to be delayed, they should run at a time.

Thanks in Advance.

Forgive me if any thing i posted is against to the rules if the forum.

plzz help me if possible as early as possible.

Robbie
20th September 2012, 11:27
How much processing is done in each of the slots you have. If all of these timer signals are being handled in the main thread then this might be your problem. Each event will only be executed once the previous one has completed.

Try splitting the processing up into multiple threads.

mahi6a1985
20th September 2012, 12:05
Hi Robbie,

In each of the SLOT a lot of processing will be done.
All the Timers will be started after a Pushbutton pressed.
All these Timers are in my function

ex:


void MainWindow::def_Values(){
timer_HMIScrLvl4 = new QTimer(this);
connect(timer_HMIScrLvl4, SIGNAL(timeout()), this, SLOT(update_HMIScrLvl4()));
timer_HMIScrLvl4->setInterval(100);

Timer_failure_display = new QTimer(this);
connect(Timer_failure_display, SIGNAL(timeout()), this, SLOT(failure_display()));
Timer_failure_display->setInterval(250);

Timer_footer = new QTimer(this);
connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
Timer_footer->setInterval(250);

Timer_diagonostic_refresh = new QTimer(this);
connect(Timer_diagonostic_refresh, SIGNAL(timeout()), this, SLOT(diagonostic_refresh()));
Timer_diagonostic_refresh->setInterval(50);

Timer_information_refresh = new QTimer(this);
connect(Timer_information_refresh, SIGNAL(timeout()), this, SLOT(information_refresh()));
Timer_information_refresh->setInterval(25);

Timer_footer = new QTimer(this);
connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
Timer_footer->setInterval(250);
}

All the above timers will started in pusbutton click event.

Santosh Reddy
21st September 2012, 08:57
Then you have seriouly re-consider your structure / architectrue of using timers.

1. Either simply the logic so that it can fit into the time
or
2. Try moving the heavy logic into a thread. (as other poster suggested)

Some how I feel you can do this with using 6 timers or more, one important rule to follow is all the slots should complete with in time. Try re-structing your timers, and what they do.

Robbie
21st September 2012, 14:16
@mahi6a1985

Do you really need 6 QTimers? I see from your code that there are two QTimers linked to the footer() slot.

I would seriously consider moving your processing to different threads.

When a QTimer emits the timeout signal, it gets put in the message queue and the slot is called by the event posting mechanism. Since all your QTimers are in the same thread, their timeout signals all get processed one after another.

If, by chance, all the QTimers timeout at exactly the same time, then, the footer() slot will be called at least 925ms after update_HMIScrLvl4(). That's assuming that each of the slots instantaneously returned and called the subsequent timeout slot.

I would suggest that you put each of your timer slots into its own thread, or group the processing by timeout interval. By this I mean that, because failure_display() and footer() are triggered every 250ms, their code could be executed in the same thread, if you like.

wysota
21st September 2012, 14:39
I have no idea what your application does but it looks to me you should seriously reconsider on how it should do it. For example you have this "information refresh" -- I don't know what it is supposed to do but assuming that at the end of its work someone is supposed to see some output from it, I strongly doubt he really needs to see it 40 times per second. From my own experience I can say that (unless you are doing something time critical, like steering a mars lander) it is enough if you refresh information on the user interface once per second or even less often. Same goes for "diagnostic refresh". Moreover you have three timers set at 250ms interval (two of which are calling the same method). Why not merge them into one timer?

pradeepreddyg95
22nd September 2012, 05:43
you can use Qt::concurrent run() functionality in your slots which will run in separate thread. and when your timers will stop ? I hope this should solve your problem ....

FunctionThreads.cpp file

void update_HMIScrLvl4(MainWindow *pWIndow)
{
/* you can access all public variables from Mainwindow class */
while(!pWIndow->m_bStop)
{
do your work after finishing "pWIndow->m_bStop = true" then again start timers
}
}
void failure_display(MainWindow *pWIndow)
{

}
void diagonostic_refresh(MainWindow *pWIndow)
{

}

MainWindow.cpp file

extern void update_HMIScrLvl4(MainWindow *pWIndow);
extern void failure_display(MainWindow *pWIndow);

void MainWindow::def_Values(){
timer_HMIScrLvl4 = new QTimer(this);
connect(timer_HMIScrLvl4, SIGNAL(timeout()), this, SLOT(update_HMIScrLvl4()));
timer_HMIScrLvl4->setInterval(100);

Timer_failure_display = new QTimer(this);
connect(Timer_failure_display, SIGNAL(timeout()), this, SLOT(failure_display()));
Timer_failure_display->setInterval(250);

Timer_footer = new QTimer(this);
connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
Timer_footer->setInterval(250);

Timer_diagonostic_refresh = new QTimer(this);
connect(Timer_diagonostic_refresh, SIGNAL(timeout()), this, SLOT(diagonostic_refresh()));
Timer_diagonostic_refresh->setInterval(50);

Timer_information_refresh = new QTimer(this);
connect(Timer_information_refresh, SIGNAL(timeout()), this, SLOT(information_refresh()));
Timer_information_refresh->setInterval(25);

Timer_footer = new QTimer(this);
connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
Timer_footer->setInterval(250);
}


void MainWindow::update_HMIScrLvl4()
{
QFuture<void>pWatcher;

if(!pWatcher.isStarted())
{
pWatcher = QtConcurrent::run(update_HMIScrLvl4,this);
}
}
void MainWindow::failure_display()
{

}

mahi6a1985
22nd September 2012, 06:59
Thanks a lot guys.. Got some interesting points from you people.

But i am sorry guys.. as i am new & also its my 1st QT application, i am unable to apply the points got from you as it is a huge application.

Cant do anything with the programing...


you can use Qt::concurrent run() functionality in your slots which will run in separate thread. and when your timers will stop ? I hope this should solve your problem ....

FunctionThreads.cpp file

void update_HMIScrLvl4(MainWindow *pWIndow)
{
/* you can access all public variables from Mainwindow class */
while(!pWIndow->m_bStop)
{
do your work after finishing "pWIndow->m_bStop = true" then again start timers
}
}
void failure_display(MainWindow *pWIndow)
{

}
void diagonostic_refresh(MainWindow *pWIndow)
{

}

MainWindow.cpp file

extern void update_HMIScrLvl4(MainWindow *pWIndow);
extern void failure_display(MainWindow *pWIndow);

void MainWindow::def_Values(){
timer_HMIScrLvl4 = new QTimer(this);
connect(timer_HMIScrLvl4, SIGNAL(timeout()), this, SLOT(update_HMIScrLvl4()));
timer_HMIScrLvl4->setInterval(100);

Timer_failure_display = new QTimer(this);
connect(Timer_failure_display, SIGNAL(timeout()), this, SLOT(failure_display()));
Timer_failure_display->setInterval(250);

Timer_footer = new QTimer(this);
connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
Timer_footer->setInterval(250);

Timer_diagonostic_refresh = new QTimer(this);
connect(Timer_diagonostic_refresh, SIGNAL(timeout()), this, SLOT(diagonostic_refresh()));
Timer_diagonostic_refresh->setInterval(50);

Timer_information_refresh = new QTimer(this);
connect(Timer_information_refresh, SIGNAL(timeout()), this, SLOT(information_refresh()));
Timer_information_refresh->setInterval(25);

Timer_footer = new QTimer(this);
connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
Timer_footer->setInterval(250);
}


void MainWindow::update_HMIScrLvl4()
{
QFuture<void>pWatcher;

if(!pWatcher.isStarted())
{
pWatcher = QtConcurrent::run(update_HMIScrLvl4,this);
}
}
void MainWindow::failure_display()
{

}


i will try this & vil get back to you @pradeep.

pradeepreddyg95
22nd September 2012, 07:53
If any compilation errors in this method plz inform us. ready to help ... :)

mahi6a1985
22nd September 2012, 08:06
Hi Pradeep,

i got this compilation error

8232

pradeepreddyg95
22nd September 2012, 08:51
give some different name to that function update_HMIScrLvl4
eg: extern void func1();

wysota
22nd September 2012, 09:03
Functions ran in threads cannot access the GUI (i.e. MainWindow). Furthermore this whole concept is simply illogical. If one has a function that takes 200ms to execute and wants to run it every 100ms then after 10 seconds one will have over 50 threads running doing the same thing, ruining each others work. Furthermore with QtConcurrent this will not work this way anyway, since QtConcurrent::run() uses the global thread pool which will very quickly dry up and those functions will not be executed every 100ms but only after a thread returns to the pool. To put it simple -- if you have a function that needs 200ms to complete, you cannot run it more often than once per 200ms, regardless if you use threads or not. You can even stand on your head and start juggling primed granades but this won't change the situation.

mahi6a1985
22nd September 2012, 09:21
I have done what u said the compilation error was gone.
but the while loop which u specified in the previous post, the i got the Compilation error now.

8235

Added after 10 minutes:


Functions ran in threads cannot access the GUI (i.e. MainWindow). Furthermore this whole concept is simply illogical. If one has a function that takes 200ms to execute and wants to run it every 100ms then after 10 seconds one will have over 50 threads running doing the same thing, ruining each others work. Furthermore with QtConcurrent this will not work this way anyway, since QtConcurrent::run() uses the global thread pool which will very quickly dry up and those functions will not be executed every 100ms but only after a thread returns to the pool. To put it simple -- if you have a function that needs 200ms to complete, you cannot run it more often than once per 200ms, regardless if you use threads or not. You can even stand on your head and start juggling primed granades but this won't change the situation.

Hi Wysota,

Dear then suggest me one example with 6 timers with different slots with different timers and they should run at a time.
-------------------------------------------------------------------------------------------------------------------------------
The main concept of my app is through tcp/ip client - server App. in which my app is client & which always sends 1 telegram every 100ms using "update_HmiScrLvl4".

and the telegram construction & values will produced by the other timers, so dat the constructed telegram can be sent to server using "update_HmiScrLvl4".

and at the same time server responses to my clients 300ms telegram (i.e., 100ms 1st telegram no reply from server. 200ms telegram no reply from server. 300ms will get response from Server to client, 400ms no reply,500ms no reply, 600ms reply from server to client ------- 900ms -----1200ms-------- ) like this i vil get response from server.

the response from server needs to evaluated using the "Timer_Information_Refresh".
-------------------------------------------------------------------------------------------------------------------------------

pradeepreddyg95
22nd September 2012, 10:35
read(), write() function should use with pMainwindow
pMainWindow->read();
pMainwindow->Write();
decleare m_bStop in mainwindow.h public
if we want we can access the GUI (mainwindow) in FunctionsThread.cpp also ... try with this you can get the result sure .....

mahi6a1985
22nd September 2012, 12:24
give some different name to that function update_HMIScrLvl4
eg: extern void func1();

ur testd.zip sample app was freezing bro when i run it.... y i cant able to know?

wysota
22nd September 2012, 13:19
Dear then suggest me one example with 6 timers with different slots with different timers and they should run at a time.
It's not a problem to have six timers and execute them in time. It's a problem when you are trying to do more than your machine can handle.

Here is a simple example that shows that you can have six timers in an app:


#include <QtCore>

class Dummy : public QObject {
Q_OBJECT
public:
Dummy(){}
public slots:
void timeout() {
QTimer *timer = qobject_cast<QTimer*>(sender());
if(!timer) return;
qDebug() << "Now is" << QDateTime::currentDateTime() << "timer id:" << timer->property("timerId").toInt() << ", interval is" << timer->interval() << "ms";
}
};

#include "main.moc"

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
Dummy d;
for(int i=0;i<6;++i) {
QTimer *timer = new QTimer(&app);
timer->setInterval(50+(qrand() % 200));
timer->setProperty("timerId", i);
QObject::connect(timer, SIGNAL(timeout()), &d, SLOT(timeout()));
timer->start();
}
return app.exec();
}


The main concept of my app is through tcp/ip client - server App. in which my app is client & which always sends 1 telegram every 100ms using "update_HmiScrLvl4".

and the telegram construction & values will produced by the other timers, so dat the constructed telegram can be sent to server using "update_HmiScrLvl4".
You don't need any threads for that nor any (not a single one!) timers. You just need to use QTcpSocket properly.

pradeepreddyg95
22nd September 2012, 17:42
i am sending the simulator which u can test ur requirment plz test it.

Added after 10 minutes:

After executing first timer timeout slot then only it will move to second timer timeout slot.
Eg:
if first timer timeout slot takes 1sec to finish process. After finishing this 1 sec the second slot will excute.

Added after 51 minutes:

using visual studio 2005 for debugging

d_stranz
22nd September 2012, 18:27
As Wysota said, if you are trying to do something in a slot that takes 1 s to finish, and you expect that slot to be executed every 100 ms, then it is more than your machine can handle, and no amount of threading or anything else is going to help that situation.

And, in your slot if you are not allowing any other events from the Qt event loop to be handled (for example, you don't call processEvents()), then the timeout signals from other timers will not be handled until after the first slot finishes. Furthermore, if the timer whose slot you are executing fires again while you are still in the slot code, that timeout will be ignored.

So, what your code is actually doing is allowing each timer to fire once every second, and the whole cycle repeats once every n seconds (n = # of timers).

mahi6a1985
24th September 2012, 05:43
i am sending the simulator which u can test ur requirment plz test it.

Added after 10 minutes:

After executing first timer timeout slot then only it will move to second timer timeout slot.
Eg:
if first timer timeout slot takes 1sec to finish process. After finishing this 1 sec the second slot will excute.

Added after 51 minutes:

using visual studio 2005 for debugging

Wat u hav explained n the code sample is good n working fine. Thanks a lot.

But wat i am asking is instead of running one after another means in ur example - After finishing this 1 sec the second slot will execute.

Instead of this, cant we run all timers together. means execute all the slots at a time wit diff intervals. Its a doubt of mine which being in my mind from many days, can't we ?.

pradeepreddyg95
24th September 2012, 10:46
what i have given code you can set different timer intervals it will work all together at a time no problem in that ... because each timer timeout slot will excute in a different thread that means if you start 2 timers with 100 ms interval two slots will execute same time in two different threads .

mahi6a1985
26th September 2012, 06:57
what i have given code you can set different timer intervals it will work all together at a time no problem in that ... because each timer timeout slot will excute in a different thread that means if you start 2 timers with 100 ms interval two slots will execute same time in two different threads .

Hi Pradeep,

After Applying your code to my huge amount of project. finally i got this error which is stopping my app y i don't know ?

8256

wysota
26th September 2012, 08:20
"I told you but you wouldn't listen"


Functions ran in threads cannot access the GUI (i.e. MainWindow).

mahi6a1985
26th September 2012, 11:00
"I told you but you wouldn't listen"

Hi Wysota,

But wat am i suppose to do now ?

I hav tried all the ways. As said by u i hav even divided my Huge SLOTs processing into small small process and tried tooo.

but all in vain nothing helping....?

at least now my urgent requirement is that my application should run "update_HMIScrLvl4" timer and execute its slot at every 100ms, Whether the other 5 timers running or not this particular "update_HMIScrLvl4" should be execute its slot every 100MS. on High Priority this particular timer should run...?

wysota
26th September 2012, 11:22
But wat am i suppose to do now ?
Read the thread from the beginning and not get fixated on one possible solution.


I hav tried all the ways.
No, you haven't. You have been ignoring what we were saying so we stopped saying anything.

I'm pretty sure you can do with one timer and no extra threads at all.

mahi6a1985
26th September 2012, 12:03
It's not a problem to have six timers and execute them in time. It's a problem when you are trying to do more than your machine can handle.

Here is a simple example that shows that you can have six timers in an app:


#include <QtCore>

class Dummy : public QObject {
Q_OBJECT
public:
Dummy(){}
public slots:
void timeout() {
QTimer *timer = qobject_cast<QTimer*>(sender());
if(!timer) return;
qDebug() << "Now is" << QDateTime::currentDateTime() << "timer id:" << timer->property("timerId").toInt() << ", interval is" << timer->interval() << "ms";
}
};

#include "main.moc"

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
Dummy d;
for(int i=0;i<6;++i) {
QTimer *timer = new QTimer(&app);
timer->setInterval(50+(qrand() % 200));
timer->setProperty("timerId", i);
QObject::connect(timer, SIGNAL(timeout()), &d, SLOT(timeout()));
timer->start();
}
return app.exec();
}


You don't need any threads for that nor any (not a single one!) timers. You just need to use QTcpSocket properly.


I hav tried this even this haven't solved my problem.

Added after 4 minutes:


Read the thread from the beginning and not get fixated on one possible solution.


No, you haven't. You have been ignoring what we were saying so we stopped saying anything.

I'm pretty sure you can do with one timer and no extra threads at all.


Dear Then tell me plz wat to do, i am running out of time... so dats d one reason n another thing i am beginner not even tried any sample "hello world" program also. I am a .net developer previously , now i am shifting to QT now. So am unable to understand wat u ppl say.

Only thing i can say is Sorry if i make u ppl think i ignored wat u said n help me Now...

wysota
26th September 2012, 14:50
I hav tried this even this haven't solved my problem.
"this" meaning what exactly? At the beginning of what you quoted I write that your problem is not related to having more than one timer and then I give you an example that you can run multiple timers without problem. If it doesn't work then it confirms what I said -- that this was not your problem.


Dear Then tell me plz wat to do, i am running out of time... so dats d one reason n another thing i am beginner not even tried any sample "hello world" program also. I am a .net developer previously , now i am shifting to QT now. So am unable to understand wat u ppl say.

First of all start using proper language instead of that trash sms speech. Then rethink your approach --- think what you really need and not what you think you need. The fact that you come from .net environment is meaningless as you wouldn't be able to do it this way in .net too. Your problem is trying to do more than your machine can handle. So instead of trying to find a way to spawn more timers (which leads to nothing), think how to obtain the same result doing less.

pradeepreddyg95
26th September 2012, 18:40
hi mahi can you post your code how you implemented

mahi6a1985
27th September 2012, 08:28
hi mahi can you post your code how you implemented
Hi Pradeep,

same i have incorporated your function names and same as it is how u have been implemented i have done that. Same as the Simulator app which u have given. You can find the Screenshot what the error i got.


"this" meaning what exactly? At the beginning of what you quoted I write that your problem is not related to having more than one timer and then I give you an example that you can run multiple timers without problem. If it doesn't work then it confirms what I said -- that this was not your problem.



First of all start using proper language instead of that trash sms speech. Then rethink your approach --- think what you really need and not what you think you need. The fact that you come from .net environment is meaningless as you wouldn't be able to do it this way in .net too. Your problem is trying to do more than your machine can handle. So instead of trying to find a way to spawn more timers (which leads to nothing), think how to obtain the same result doing less.


Dear Sir ,

I know that is what the problem is. For that what i have to do i am asking. if, is their any solution then help me Else Thanks for sharing the knowledge still now.

wysota
27th September 2012, 09:48
I know that is what the problem is. For that what i have to do i am asking. if, is their any solution then help me Else Thanks for sharing the knowledge still now.

I can't suggest anything without knowing what each of the routines you are running is supposed to do. I already suggested some things earlier in this thread -- reduce frequency of your updates. If you post the actual code you have in the routines maybe I can suggest more.

pradeepreddyg95
27th September 2012, 09:49
if you are displaying the screen shot i cant estimate how you implement if you post your code i can help ...

mahi6a1985
27th September 2012, 12:08
I can't suggest anything without knowing what each of the routines you are running is supposed to do. I already suggested some things earlier in this thread -- reduce frequency of your updates. If you post the actual code you have in the routines maybe I can suggest more.

Ok Thanks and sorry cant post my code. Can i know how to calculate, how much time is taken to execute a slot. As i am having so much of processing in each slots, may be my timeout intervals are not working i think. So i want to know how much time it takes for each slot to start and end ?

wysota
27th September 2012, 12:23
Can i know how to calculate, how much time is taken to execute a slot.
If you want a precise value, you have to use some profiler (such as callgrind on Linux). If you just need an estimate, create a QTime object at the beginning of the slot, start it and at the end of the slot print the value returned by QTime::elapsed() to the console.

mahi6a1985
3rd October 2012, 06:24
If you want a precise value, you have to use some profiler (such as callgrind on Linux). If you just need an estimate, create a QTime object at the beginning of the slot, start it and at the end of the slot print the value returned by QTime::elapsed() to the console.

Hi Wysota,
using your code i came to know that all of my slots connected to timers are executed(i.e. Each Slot start to End of it.) below 10MS. Thanks for this.
But now what i want is that each slots should be executed every 500ms. means the slots should be called every 500ms some of them and some them every 250ms. Can you suggest me anything please.

So this means ---

1st timer SLOT to be called every 500ms and for execution of slot it takes 6ms.
2nd timer SLOT to be called every 600ms and for execution of slot it takes 4ms.
3rd timer SLOT to be called every 100ms and for the execution of slot it takes 4ms.
4th timer SLOT to be called every 1000ms and for the execution of slot it takes 6ms.
5th timer SLOT to be called every 3000ms and for the execution of slot it takes 3ms.
6th timer SLOT to be called every 5000ms and for the execution of slot it takes 2ms.

Note :- The execution of slot time i given above is calculated using the QTime::elapsed() for each of my slots.

Thanks in Advance.

ChrisW67
3rd October 2012, 07:50
You already have everything you need in this thread. Here it is laid out in verbose code. One thread, six timers, six processes (slots):


#include <QtCore>
#include <QDebug>

class Dummy: public QObject
{
Q_OBJECT
public:
Dummy(QObject *p = 0): QObject(p)
{
connect(&t1, SIGNAL(timeout()), SLOT(p1()));
connect(&t2, SIGNAL(timeout()), SLOT(p2()));
connect(&t3, SIGNAL(timeout()), SLOT(p3()));
connect(&t4, SIGNAL(timeout()), SLOT(p4()));
connect(&t5, SIGNAL(timeout()), SLOT(p5()));
connect(&t6, SIGNAL(timeout()), SLOT(p6()));

time.start();
t1.start(500);
t2.start(600);
t3.start(100);
t4.start(1000);
t5.start(3000);
t6.start(5000);
}

public slots:
void p1() { qDebug() << "p1 at" << time.elapsed(); usleep(6000); }
void p2() { qDebug() << "p2 at" << time.elapsed(); usleep(4000); }
void p3() { qDebug() << "p3 at" << time.elapsed(); usleep(4000); }
void p4() { qDebug() << "p4 at" << time.elapsed(); usleep(6000); }
void p5() { qDebug() << "p5 at" << time.elapsed(); usleep(3000); }
void p6() { qDebug() << "p6 at" << time.elapsed(); usleep(2000); }

private:
QTime time;
QTimer t1, t2, t3, t4, t5, t6;
};

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Dummy d;
return app.exec();
}
#include "main.moc"


You have timers that, in theory at least, will fire together but may be delayed by a few milliseconds while another timer slot is finishing. You will see that in the output.

You can do this particular task with one timer running at 100 milliseconds intervals.

mahi6a1985
3rd October 2012, 09:25
Hey Thanks a lot Chris, I will implement it in my Application and will let u know. Hope it works in my App too as u suggested.



You can do this particular task with one timer running at 100 milliseconds intervals.

How to implement it in One Timer with 100ms can i know ?

mahi6a1985
4th October 2012, 11:05
Hi Wysota,
using your code i came to know that all of my slots connected to timers are executed(i.e. Each Slot start to End of it.) below 10MS. Thanks for this.
But now what i want is that each slots should be executed every 500ms. means the slots should be called every 500ms some of them and some them every 250ms. Can you suggest me anything please.

So this means ---

1st timer SLOT to be called every 500ms and for execution of slot it takes 6ms.
2nd timer SLOT to be called every 600ms and for execution of slot it takes 4ms.
3rd timer SLOT to be called every 100ms and for the execution of slot it takes 4ms.
4th timer SLOT to be called every 1000ms and for the execution of slot it takes 6ms.
5th timer SLOT to be called every 3000ms and for the execution of slot it takes 3ms.
6th timer SLOT to be called every 5000ms and for the execution of slot it takes 2ms.

Note :- The execution of slot time i given above is calculated using the QTime::elapsed() for each of my slots.

Thanks in Advance.

Hi Wysota,

please suggest me any solution for the above. and one more thing in QT tips i found a point "Keep response times short" what does it mean can i have a sample app, if possible.

Thanks in Advance

wysota
4th October 2012, 16:58
please suggest me any solution for the above
A solution for what?


and one more thing in QT tips i found a point "Keep response times short" what does it mean
It means you should make your functions short and fast and not long and slow to avoid freezing the user interface (and possibly the whole system).

can i have a sample app, if possible.
No but you can read this article: Keeping the GUI Responsive.

mahi6a1985
4th October 2012, 17:59
hi Wysota,

The solution is for my the app description i mention above-
means,
1st timer SLOT to be called every 500ms and for execution of slot it takes 6ms.
2nd timer SLOT to be called every 600ms and for execution of slot it takes 4ms.
3rd timer SLOT to be called every 100ms and for the execution of slot it takes 4ms.
4th timer SLOT to be called every 1000ms and for the execution of slot it takes 6ms.
5th timer SLOT to be called every 3000ms and for the execution of slot it takes 3ms.
6th timer SLOT to be called every 5000ms and for the execution of slot it takes 2ms.

Note :- The execution of slot time i given above is calculated using the QTime::elapsed() for each of my slots.

wysota
4th October 2012, 18:45
Start one timer with 100ms interval and call appropriate functions from the timer's timeout slot. Calculate time using QTime::elapsed() rathar than counting how many times the timeout slot was called.

ChrisW67
5th October 2012, 00:18
please suggest me any solution for the above.
:confused::confused: I have already given you exactly this. Several people had given you all the necessary information to do it yourself before that. Why do you keep asking the same question over and over? Are you actually understanding anything or just hoping for a cut'n'paste magic bullet? Please understand that we are not going to come to your office and write your program for you.


How to implement it in One Timer with 100ms can i know ?
In the slot keep a count of the number of times the timer has elapsed, or use QTime::elapsed(), and:

every time the timer elapses do task 3,
every 5th time also do task 1,
every 6th time also do task 2,
every 10th time also do task 4,
every 30th time also do task 5,
every 50th time also do task 6.

No. I will not write the code for this. I don't think you understand how the naïve version I gave you works, and this requires more understanding.

mahi6a1985
5th October 2012, 04:48
:confused::confused: I have already given you exactly this. Several people had given you all the necessary information to do it yourself before that. Why do you keep asking the same question over and over? Are you actually understanding anything or just hoping for a cut'n'paste magic bullet? Please understand that we are not going to come to your office and write your program for you.


In the slot keep a count of the number of times the timer has elapsed, or use QTime::elapsed(), and:

every time the timer elapses do task 3,
every 5th time also do task 1,
every 6th time also do task 2,
every 10th time also do task 4,
every 30th time also do task 5,
every 50th time also do task 6.

No. I will not write the code for this. I don't think you understand how the naïve version I gave you works, and this requires more understanding.

Ho ho what do you mean by this "Are you actually understanding anything or just hoping for a cut'n'paste magic bullet? Please understand that we are not going to come to your office and write your program for you."

As i am beginner i am asking for clarification of each, each time you people gave a suggestion i am learning a new thing. that's the reason i am asking for examples.
anyway thanks for nice understanding. Thanks To FORUM.


Start one timer with 100ms interval and call appropriate functions from the timer's timeout slot. Calculate time using QTime::elapsed() rathar than counting how many times the timeout slot was called.

Hi Wysota,

Thanks a lot Sir, i have got so much of information & knowledge from your posts not only in this thread even the other threads i have seen your posts. They were good and helpful for beginners like me. Thanks a lot.

wysota
5th October 2012, 07:46
Ho ho what do you mean by this "Are you actually understanding anything or just hoping for a cut'n'paste magic bullet? Please understand that we are not going to come to your office and write your program for you."

As i am beginner i am asking for clarification of each, each time you people gave a suggestion i am learning a new thing. that's the reason i am asking for examples.
anyway thanks for nice understanding.
Chris means that you have been given answer to your question many times in this thread now and you were ignoring it and asking the question again and again. The fact that you are a beginner is not something that should make us go easier on you -- it should make you go harder on yourself. You should be writing your own examples as much as possible, otherwise you will always be a beginner. We will happily correct your code but you need to do your own coding.




Thanks a lot Sir, i have got so much of information & knowledge from your posts not only in this thread even the other threads i have seen your posts. They were good and helpful for beginners like me. Thanks a lot.
Unfortunately buttering me up won't help here. I second every word Chris said in his post.

mahi6a1985
5th October 2012, 13:23
Chris means that you have been given answer to your question many times in this thread now and you were ignoring it and asking the question again and again. The fact that you are a beginner is not something that should make us go easier on you -- it should make you go harder on yourself. You should be writing your own examples as much as possible, otherwise you will always be a beginner. We will happily correct your code but you need to do your own coding.




Unfortunately buttering me up won't help here. I second every word Chris said in his post.

lol buttering for what. Anyway still i wanna thank you for the support. Because of you i came to know what exactly the problem in my application. So hope i can solve it by myself.

Thanks a lot to one and all and forum. Finally got it what the problem is so will try myself.