Results 1 to 6 of 6

Thread: Handler thread to process messages asynchronously

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Handler thread to process messages asynchronously

    Hi! I'm in the need of writing a thread that continuously processes messages, one after the other (and maybe with a priority), coming from other threads.
    I used something similar in an Android application using the Handler class (http://developer.android.com/referen...s/Handler.html). Is there anything similar in Qt or should I implement it myself using queues, Semaphores etc...?
    What I found so far is the QAbstractMessageHandler, but it doesn't seem to be what I'm looking for.
    Is there anything else that may be of help to implement such a thing?
    Thanks for any advice!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Handler thread to process messages asynchronously

    Have a look at QEventLoop.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Handler thread to process messages asynchronously

    May you give me a little more information about how to use the QEventLoop class in this context?

    I read some other threads around and found this way: I create a handler object that responds to requests using slots. I instantiate this object in a new thread and I send requests from other threads by using QMetaObject::invokeMethod passing Qt::QueuedConnection or using signals and slots.

    Do you consider this approach good?
    Thank you for the information!

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Handler thread to process messages asynchronously

    You can use signals and slots or you can have a look at QApplication::postEvent. Since you'll only have one consumer in this context, the later seems more logical - you would send the events directly to your handler thread rather than the handler thread having to connect to your signals.

  5. #5
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Handler thread to process messages asynchronously

    This is somehow what you were talking about? Or am I completely off the way?

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QEventLoop>
    3. #include <QWaitCondition>
    4. #include <QMutex>
    5. #include <QThread>
    6.  
    7. class MyEventLoop : public QThread
    8. {
    9. public:
    10. MyEventLoop() {}
    11. bool event(QEvent* event)
    12. {
    13. qDebug("Processing event.");
    14. return true;
    15. }
    16. void run() {exec();}
    17. };
    18.  
    19. int main(int argc, char* argv[])
    20. {
    21. QCoreApplication a(argc, argv);
    22. QThread thread;
    23. thread.start();
    24.  
    25. for (int i = 0; i < 10; i++) {
    26. QMutex mutex;
    27. cond.wait(&mutex, qrand() % 4000);
    28.  
    29. qDebug("Sending event.");
    30. QCoreApplication::postEvent(&thread, new QEvent(QEvent::Hide));
    31. }
    32.  
    33. return a.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

    I would like to use this even following the command design pattern, so sending requests in the form of objects to better describe the request. Sill this is a good way to go? I suppose I have to subclass QEvent to add whatever else I need right?

    Speaking about memory management, the documentation obviously tell me to allocate the object in the heap, but does it free memory automatically after the event(...) method has returned true or am I supposed to do it there myself?

    The approach I reported in the other post was correct as well?

    Thanks!
    Last edited by Luc4; 24th April 2011 at 12:22.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Handler thread to process messages asynchronously

    Yes, like that.

    So your constructor would be like so:

    Qt Code:
    1. myEvent::myEvent(int yourData) : QEvent(QEvent::User)
    2. {
    3. this->yourData = yourData; // etc...
    4. }
    To copy to clipboard, switch view to plain text mode 

    and then:

    Qt Code:
    1. bool myThread::event( QEvent * e )
    2. {
    3. if (e->type() >= QEvent::User)
    4. {
    5. myEvent *e = static_cast<myEvent*>(e);
    6. (...)
    To copy to clipboard, switch view to plain text mode 

    As stated in the documentation, you must not access the object again once it has been posted:

    The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted. It is not safe to access the event after it has been posted.
    Last edited by squidge; 24th April 2011 at 15:12.

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

    Luc4 (24th April 2011)

Similar Threads

  1. creating widgets asynchronously
    By tampstaffs in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2009, 10:30
  2. How to communicate Qt Process with non-qt process
    By nrabara in forum Qt for Embedded and Mobile
    Replies: 9
    Last Post: 15th February 2009, 21:01
  3. Asynchronously playing a sound (.wav) on windows mobile
    By TMan in forum Qt for Embedded and Mobile
    Replies: 10
    Last Post: 20th October 2008, 19:16
  4. GUI Thread getting no time to process
    By steg90 in forum Qt Programming
    Replies: 11
    Last Post: 9th May 2007, 09:29
  5. Qt4 no debug messages
    By TheKedge in forum Newbie
    Replies: 3
    Last Post: 23rd January 2006, 17:52

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.