Results 1 to 2 of 2

Thread: thread Synchronous problem

  1. #1
    Join Date
    Apr 2014
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default thread Synchronous problem

    i start a thread to handle somethings ,gui wait for the things process .when end , notify the gui thread go run .here is my code :
    Qt Code:
    1. class Worker : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. Worker (){m_hEvent = NULL ;};
    6. public slots:
    7. void doWork(const QString &parameter) {
    8. // ...
    9.  
    10.  
    11. setEvent(m_hEvent);
    12. }
    13.  
    14. public :
    15. void SetSyncEvent(Handle hEvent){m_hEvent = hEvent};
    16. signals:
    17. void resultReady(const QString &result);
    18. private:
    19. Handle m_hEvent ;
    20. };
    21.  
    22. class Controller : public QObject
    23. {
    24. Q_OBJECT
    25. QThread workerThread;
    26. public:
    27. Controller() {
    28.  
    29. m_hEvent = ::CreateEvent(NULL,FALSE,FALSE,NULL);
    30.  
    31. Worker *worker = new Worker;
    32.  
    33. worker->SetEvent(m_hEvent);
    34. worker->moveToThread(&workerThread);
    35. connect(workerThread, &QThread::finished, worker, &QObject::deleteLater);
    36. connect(this, &Controller::operate, worker, &Worker::doWork);
    37. // connect(worker, &Worker::resultReady, this, &Controller::handleResults);
    38. workerThread.start();
    39. }
    40. ~Controller() {
    41. workerThread.quit();
    42. workerThread.wait();
    43. }
    44. void Oper()
    45. {
    46. emit operate(QString("fsfs"));
    47. WaitForSingleObject(m_hEvent,INFINITE);
    48. .......
    49. }
    50. public slots:
    51. // void handleResults(const QString &);
    52. signals:
    53. void operate(const QString &);
    54. private:
    55. Handle m_hEvent ;
    56. };
    57.  
    58. void main()
    59. { Controller controle ; controle.oper();
    60. }
    To copy to clipboard, switch view to plain text mode 

    but sometimes i found after worker class’s function doWork run the last code setevent(m_hevent), the app can not get the event and the WaitForSingleObject(m_hEvent,INFINITE) always in wait state . i use vs2008 qt 5.2.1 ,i found the workerThread can not exit the thread ,can anyone tell me why and how can i solved this problem .
    Last edited by anda_skoa; 10th May 2014 at 11:10. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: thread Synchronous problem

    First question is why you even want to use a thread if you block the main thread anyway?

    Usually a worker thread is used to do something long going and potentially block while keeping the UI responsive.

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 17th October 2013, 11:12
  2. QtSerialPort synchronous approach
    By codein in forum Newbie
    Replies: 2
    Last Post: 4th September 2012, 09:45
  3. Synchronous QFtp?
    By aarpon in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2009, 09:28
  4. problem with Synchronous Http request
    By rchaitanya in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 30th January 2009, 11:03
  5. Synchronous Http (Good reason)
    By umbrella in forum Qt Programming
    Replies: 5
    Last Post: 4th April 2008, 14:14

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.