Results 1 to 6 of 6

Thread: Unable to abort the worker thread using singleshot timer

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Unable to abort the worker thread using singleshot timer

    I have a Worker class with doWork() function/slot where in I do stuff using while loop coupled with QThread. I'm trying to break out of loop using a singleshot timer but doesn't seem to be working though.
    Could someone guide me what's wrong in my code. Thanks.

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QThread>
    3. #include <QTime>
    4. #include <QTimer>
    5. #include <QDebug>
    6.  
    7. class Worker : public QObject
    8. {
    9. Q_OBJECT
    10. int m_continue;
    11. public:
    12. Worker() : m_continue(1)
    13. {
    14. qDebug() << "Worker::Ctor";
    15. QTimer::singleShot(7000, this, SLOT(discontinue())); // Expecting to call discontinue() after 7 secs.
    16. }
    17. ~Worker() { qDebug() << "Worker::Dtor"; }
    18. private slots:
    19. void doWork()
    20. {
    21. while(m_continue)
    22. {
    23. qDebug() << QTime::currentTime().toString("HH:mm");
    24. QThread::sleep(2);
    25. }
    26. emit done();
    27. qDebug() << "Done!";
    28. }
    29. void discontinue()
    30. {
    31. qDebug() << "Aborting ...";
    32. m_continue = 0;
    33. }
    34. signals:
    35. void done();
    36. };
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QCoreApplication a(argc, argv);
    41. QThread thread;
    42. Worker worker;
    43. worker.moveToThread(&thread);
    44. QCoreApplication::connect(&thread, SIGNAL(started()), &worker, SLOT(doWork()));
    45. QCoreApplication::connect(&worker, SIGNAL(done()), &thread, SLOT(quit()));
    46. QCoreApplication::connect(&worker, SIGNAL(done()), &worker, SLOT(deleteLater()));
    47. QCoreApplication::connect(&thread, SIGNAL(finished()), &thread, SLOT(deleteLater()));
    48. thread.start();
    49.  
    50. return a.exec();
    51. }
    52.  
    53. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unable to abort the worker thread using singleshot timer

    Try to change dWork to this.
    Qt Code:
    1. void doWork()
    2. {
    3. if(m_continue)
    4. {
    5. qDebug() << QTime::currentTime().toString("HH:mm");
    6. QTimer::singleShotQThread(2000, this, SLOT(doWork()));
    7. }
    8. else
    9. {
    10. emit done();
    11. qDebug() << "Done!";
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    While loop is blocking event loop in thread.

  3. The following user says thank you to Lesiok for this useful post:

    rawfool (5th July 2017)

  4. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Unable to abort the worker thread using singleshot timer

    Thank you.

  5. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Unable to abort the worker thread using singleshot timer

    Given the scenario with QThreads implementation using Worker object, what would be the correct way to implement a QThread to run continuously and with a option to abort it when required.
    What I looking for is, if the above method just enough or is there a better way to do it?
    Thanks!
    Last edited by rawfool; 7th July 2017 at 07:47.

  6. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Unable to abort the worker thread using singleshot timer

    It is simple & sufficient.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. The following user says thank you to Santosh Reddy for this useful post:

    rawfool (9th July 2017)

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unable to abort the worker thread using singleshot timer

    You have to remember : if You want to communicate with Worker via signals and slots You can not block thread event loop.

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

    rawfool (9th July 2017)

Similar Threads

  1. Replies: 1
    Last Post: 26th November 2016, 19:01
  2. Replies: 4
    Last Post: 17th October 2013, 11:12
  3. QTimer in a worker thread
    By inger in forum Newbie
    Replies: 6
    Last Post: 2nd November 2012, 12:37
  4. Worker thread
    By doggrant in forum Newbie
    Replies: 4
    Last Post: 3rd November 2009, 15:49
  5. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 22:09

Tags for this Thread

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.