Multiple timers in single Application
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??
Re: Multiple timers in single Application
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?
Re: Multiple timers in single Application
:)
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!!!!!!!
Re: Multiple timers in single Application
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.
Re: Multiple timers in single Application
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
Re: Multiple timers in single Application
Quote:
Originally Posted by
Santosh Reddy
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 -
Quote:
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.
Re: Multiple timers in single Application
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.
Re: Multiple timers in single Application
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:
Quote:
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.
Re: Multiple timers in single Application
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.
Re: Multiple timers in single Application
@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.
Re: Multiple timers in single Application
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?
Re: Multiple timers in single Application
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()
{
}
Re: Multiple timers in single Application
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...
Quote:
Originally Posted by
pradeepreddyg95
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.
Re: Multiple timers in single Application
If any compilation errors in this method plz inform us. ready to help ... :)
1 Attachment(s)
Re: Multiple timers in single Application
Hi Pradeep,
i got this compilation error
Attachment 8232
3 Attachment(s)
Re: Multiple timers in single Application
give some different name to that function update_HMIScrLvl4
eg: extern void func1();
Re: Multiple timers in single Application
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.
1 Attachment(s)
Re: Multiple timers in single Application
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.
Attachment 8235
Added after 10 minutes:
Quote:
Originally Posted by
wysota
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".
-------------------------------------------------------------------------------------------------------------------------------
Re: Multiple timers in single Application
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 .....
Re: Multiple timers in single Application
Quote:
Originally Posted by
pradeepreddyg95
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?