Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48

Thread: Multiple timers in single Application

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple timers in single Application

    Quote Originally Posted by mahi6a1985 View Post
    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:

    Qt Code:
    1. #include <QtCore>
    2.  
    3. class Dummy : public QObject {
    4. Q_OBJECT
    5. public:
    6. Dummy(){}
    7. public slots:
    8. void timeout() {
    9. QTimer *timer = qobject_cast<QTimer*>(sender());
    10. if(!timer) return;
    11. qDebug() << "Now is" << QDateTime::currentDateTime() << "timer id:" << timer->property("timerId").toInt() << ", interval is" << timer->interval() << "ms";
    12. }
    13. };
    14.  
    15. #include "main.moc"
    16.  
    17. int main(int argc, char **argv) {
    18. QCoreApplication app(argc, argv);
    19. Dummy d;
    20. for(int i=0;i<6;++i) {
    21. QTimer *timer = new QTimer(&app);
    22. timer->setInterval(50+(qrand() % 200));
    23. timer->setProperty("timerId", i);
    24. QObject::connect(timer, SIGNAL(timeout()), &d, SLOT(timeout()));
    25. timer->start();
    26. }
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. The following user says thank you to wysota for this useful post:

    mahi6a1985 (24th September 2012)

  3. #22
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    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
    Attached Files Attached Files
    Last edited by pradeepreddyg95; 22nd September 2012 at 17:42.

  4. The following user says thank you to pradeepreddyg95 for this useful post:

    mahi6a1985 (24th September 2012)

  5. #23
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multiple timers in single Application

    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).

  6. The following user says thank you to d_stranz for this useful post:

    mahi6a1985 (24th September 2012)

  7. #24
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by pradeepreddyg95 View Post
    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 ?.

  8. #25
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    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 .

  9. The following user says thank you to pradeepreddyg95 for this useful post:

    mahi6a1985 (24th September 2012)

  10. #26
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by pradeepreddyg95 View Post
    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 ?

    ghgfh.jpg

  11. #27
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple timers in single Application

    "I told you but you wouldn't listen"

    Quote Originally Posted by wysota View Post
    Functions ran in threads cannot access the GUI (i.e. MainWindow).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #28
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by wysota View Post
    "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...?

  13. #29
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple timers in single Application

    Quote Originally Posted by mahi6a1985 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #30
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by wysota View Post
    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:

    Qt Code:
    1. #include <QtCore>
    2.  
    3. class Dummy : public QObject {
    4. Q_OBJECT
    5. public:
    6. Dummy(){}
    7. public slots:
    8. void timeout() {
    9. QTimer *timer = qobject_cast<QTimer*>(sender());
    10. if(!timer) return;
    11. qDebug() << "Now is" << QDateTime::currentDateTime() << "timer id:" << timer->property("timerId").toInt() << ", interval is" << timer->interval() << "ms";
    12. }
    13. };
    14.  
    15. #include "main.moc"
    16.  
    17. int main(int argc, char **argv) {
    18. QCoreApplication app(argc, argv);
    19. Dummy d;
    20. for(int i=0;i<6;++i) {
    21. QTimer *timer = new QTimer(&app);
    22. timer->setInterval(50+(qrand() % 200));
    23. timer->setProperty("timerId", i);
    24. QObject::connect(timer, SIGNAL(timeout()), &d, SLOT(timeout()));
    25. timer->start();
    26. }
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 


    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:


    Quote Originally Posted by wysota View Post
    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...
    Last edited by mahi6a1985; 26th September 2012 at 12:03.

  15. #31
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple timers in single Application

    Quote Originally Posted by mahi6a1985 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    mahi6a1985 (27th September 2012)

  17. #32
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    hi mahi can you post your code how you implemented

  18. #33
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by pradeepreddyg95 View Post
    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.

    Quote Originally Posted by wysota View Post
    "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.

  19. #34
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple timers in single Application

    Quote Originally Posted by mahi6a1985 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #35
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    if you are displaying the screen shot i cant estimate how you implement if you post your code i can help ...

  21. The following user says thank you to pradeepreddyg95 for this useful post:

    mahi6a1985 (27th September 2012)

  22. #36
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by wysota View Post
    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 ?

  23. #37
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple timers in single Application

    Quote Originally Posted by mahi6a1985 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  24. The following user says thank you to wysota for this useful post:

    mahi6a1985 (27th September 2012)

  25. #38
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by wysota View Post
    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.
    Last edited by mahi6a1985; 3rd October 2012 at 06:31.

  26. #39
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multiple timers in single Application

    You already have everything you need in this thread. Here it is laid out in verbose code. One thread, six timers, six processes (slots):
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. class Dummy: public QObject
    5. {
    6. Q_OBJECT
    7. public:
    8. Dummy(QObject *p = 0): QObject(p)
    9. {
    10. connect(&t1, SIGNAL(timeout()), SLOT(p1()));
    11. connect(&t2, SIGNAL(timeout()), SLOT(p2()));
    12. connect(&t3, SIGNAL(timeout()), SLOT(p3()));
    13. connect(&t4, SIGNAL(timeout()), SLOT(p4()));
    14. connect(&t5, SIGNAL(timeout()), SLOT(p5()));
    15. connect(&t6, SIGNAL(timeout()), SLOT(p6()));
    16.  
    17. time.start();
    18. t1.start(500);
    19. t2.start(600);
    20. t3.start(100);
    21. t4.start(1000);
    22. t5.start(3000);
    23. t6.start(5000);
    24. }
    25.  
    26. public slots:
    27. void p1() { qDebug() << "p1 at" << time.elapsed(); usleep(6000); }
    28. void p2() { qDebug() << "p2 at" << time.elapsed(); usleep(4000); }
    29. void p3() { qDebug() << "p3 at" << time.elapsed(); usleep(4000); }
    30. void p4() { qDebug() << "p4 at" << time.elapsed(); usleep(6000); }
    31. void p5() { qDebug() << "p5 at" << time.elapsed(); usleep(3000); }
    32. void p6() { qDebug() << "p6 at" << time.elapsed(); usleep(2000); }
    33.  
    34. private:
    35. QTime time;
    36. QTimer t1, t2, t3, t4, t5, t6;
    37. };
    38.  
    39. int main(int argc, char **argv)
    40. {
    41. QCoreApplication app(argc, argv);
    42. Dummy d;
    43. return app.exec();
    44. }
    45. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    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.

  27. The following user says thank you to ChrisW67 for this useful post:

    mahi6a1985 (3rd October 2012)

  28. #40
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    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.

    Quote Originally Posted by ChrisW67 View Post
    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 ?
    Last edited by mahi6a1985; 3rd October 2012 at 10:14.

Similar Threads

  1. Single Application Instance
    By BadKnees in forum Qt Programming
    Replies: 8
    Last Post: 4th November 2014, 14:57
  2. Multiple project files in a single directory
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 17th July 2008, 07:03
  3. Single QGLContext across multiple QGLWidgets
    By pseudosig in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2008, 23:13
  4. Single slot for multiple list boxes
    By Sheetal in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 06:53
  5. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.