Results 1 to 2 of 2

Thread: Running Thread multiple Times

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Running Thread multiple Times

    Hi,
    I am create a simple window like attached image.
    windowForm.png

    The algorithm is like this. When user move the scrollbar, the program will create a thread using scrollbar index as the ID.
    I am try to create worker class as an object like below :

    worker.h
    Qt Code:
    1. #ifndef WORKER_H
    2. #define WORKER_H
    3.  
    4. #include <QObject>
    5. #include <QMutex>
    6.  
    7. class workerThread;
    8. class Worker : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit Worker(QObject *parent = 0);
    14. void requestWork(int id);
    15. void abort();
    16. void run();
    17. void runThread(int id);
    18. private:
    19. bool _abort;
    20. bool _working;
    21. QMutex mutex;
    22. int ID;
    23. bool prepareThread(workerThread *&thread);
    24. workerThread *thisThread;
    25. signals:
    26. void workRequested();
    27. void valueChanged(const QString &value);
    28. void finished();
    29.  
    30. public slots:
    31. void doWork();
    32. void thisThreadStarted();
    33. void thisThreadFinished();
    34. };
    35.  
    36. #include <QThread>
    37. class workerThread : public QThread
    38. {
    39. Q_OBJECT
    40. public:
    41. workerThread(Worker *workerThr):workerThr(workerThr){}
    42. virtual ~workerThread(){}
    43.  
    44. void run() {workerThr->run();}
    45.  
    46. protected:
    47. Worker *workerThr;
    48. };
    49.  
    50. #endif // WORKER_H
    To copy to clipboard, switch view to plain text mode 

    worker.cpp
    Qt Code:
    1. #include "worker.h"
    2. #include <QTimer>
    3. #include <QEventLoop>
    4.  
    5. #include <QThread>
    6. #include <QDebug>
    7.  
    8. Worker::Worker(QObject *parent) :
    9. QObject(parent)
    10. {
    11. _working =false;
    12. _abort = false;
    13. thisThread = NULL;
    14. }
    15.  
    16. void Worker::abort()
    17. {
    18. mutex.lock();
    19. if (_working)
    20. {
    21. _abort = true;
    22. //qDebug()<<"Abort "<<QString::number(ID);
    23. }
    24. mutex.unlock();
    25. }
    26.  
    27. void Worker::run()
    28. {
    29. doWork();
    30. }
    31.  
    32. void Worker::runThread(int id)
    33. {
    34. //abort thread
    35. abort();
    36.  
    37. //check if thread still run
    38. //check thread every 10ms
    39. while(thisThread)
    40. {
    41. QEventLoop loop;
    42. QTimer::singleShot(10, &loop, SLOT(quit()));
    43. loop.exec();
    44. }
    45.  
    46. mutex.lock();
    47. _working = true;
    48. _abort = false;
    49. ID = id;
    50. mutex.unlock();
    51.  
    52. //run the new thread
    53. thisThread = NULL;
    54. prepareThread(thisThread);
    55. thisThread->start();
    56. }
    57.  
    58.  
    59. void Worker::doWork()
    60. {
    61. qDebug()<<"Process "<<QString::number(ID);
    62. for (int i = 0; i < 10; i ++)
    63. {
    64. // Checks if the process should be aborted
    65. mutex.lock();
    66. bool abort = _abort;
    67. mutex.unlock();
    68.  
    69. if (abort)
    70. {
    71. qDebug()<<"Aborting "<<QString::number(ID);
    72. break;
    73. }
    74. else
    75. {
    76. // add very heavy process
    77. QEventLoop loop;
    78. QTimer::singleShot(200, &loop, SLOT(quit()));
    79. loop.exec();
    80. }
    81. }
    82.  
    83. // Set _working to false, meaning the process can't be aborted anymore.
    84. mutex.lock();
    85. _working = false;
    86. mutex.unlock();
    87.  
    88. qDebug()<<"Finished "<<QString::number(ID);
    89. emit finished();
    90. }
    91.  
    92. bool Worker::prepareThread(workerThread *&thread)
    93. {
    94. thread = new workerThread(this);
    95. connect(thread, SIGNAL(started()), this,
    96. SLOT(thisThreadStarted()), Qt::BlockingQueuedConnection);
    97. connect(thread, SIGNAL(finished()), this,
    98. SLOT(thisThreadFinished()), Qt::BlockingQueuedConnection);
    99. connect(thread, SIGNAL(terminated()), this,
    100. SLOT(thisThreadFinished()), Qt::BlockingQueuedConnection);
    101.  
    102. return true;
    103. }
    104.  
    105. void Worker::thisThreadStarted()
    106. {
    107.  
    108. }
    109.  
    110. void Worker::thisThreadFinished()
    111. {
    112. thisThread = NULL;
    113. }
    To copy to clipboard, switch view to plain text mode 

    But, I get the problem. When I move the scroolbar to another index (from 1 to 314). I hope the program process the last 314th index when I dont move scrollbar again. But, I get the debug message the program still process the previous index.
    Aborting "8"
    Finished "8"
    Process "314"
    Finished "314" <--- I hope the program stop in here and not process the previous ID
    Process "311"
    Finished "311"
    Process "309"
    Finished "309"
    Process "307"
    Finished "307"
    Process "304"
    Finished "304"

    Please help me how to solve this problem.
    I have attached my full code.
    Thank you for your attention.

    best regards,

    totosugito
    Attached Files Attached Files

Similar Threads

  1. How to use a class multiple times?
    By xtlc in forum Newbie
    Replies: 5
    Last Post: 8th August 2013, 15:48
  2. Replies: 8
    Last Post: 28th March 2013, 01:12
  3. Hiding and showing the same mainwindow multiple times
    By Charvi in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2012, 12:19
  4. Can I call setRange multiple times for QProgressBar ?
    By elizabeth.h1 in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2010, 07:18
  5. Contex menu appear multiple times
    By kubas in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2009, 14:55

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.