Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Good way to wait until processing a event is done?

[Solved] Good way to wait until processing a event is done?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 8.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    augiekim
    wrote on last edited by
    #1

    I'm writing a multi-threaded code.
    In my code, a thread should wait until QCoreApplication::postEvent is done.
    I think QMutex is not a good way to do it because A QMutex must be unlocked in the same thread that locked it.
    And I cannot use QCoreApplication::sendEvent because there is some UI thread depended jobs on the event handler's side.

    @MyEvent event = new MyEvent();
    QCoreApplication::postEvent(object, event);
    // wait the event is processed...
    // do next thing here..
    @

    Maybe I can send a reference of local variable (such as bool type) via event object and wait the variable is changed, like below, but I wand to use some more elegant way.

    @bool processed = false;
    MyEvent event = new MyEvent(processed);
    QCoreApplication::postEvent(object, event);
    // wait the event is processed...
    while(!processed)
    {
    sleep(1);
    }
    // do next thing here..
    @

    @class MyEvent:public QEvent
    {
    public:
    MyEvent(bool& flag):QEvent((QEvent::Type)MY_EVENT), processed(flag)
    {
    }
    virtual ~MyEvent() {processed = true;};
    private:
    bool& processed;
    };
    @

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      If you must wait until the event has been processed and sendEvent cannot be used, you should reconsider whether you actually have to wait for the event to be processed.

      What is the purpose of this exercise? What do you want to achieve (not the waiting part, the bigger picture)?

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        augiekim
        wrote on last edited by
        #3

        [quote author="Franzk" date="1305088214"]If you must wait until the event has been processed and sendEvent cannot be used, you should reconsider whether you actually have to wait for the event to be processed.

        What is the purpose of this exercise? What do you want to achieve (not the waiting part, the bigger picture)?[/quote]

        The thread procedure is created by an open-source library for a specific network protocol (XMPP) and the event is the first notification from the thread which requests to create a new UI to display the following events from the thread before the method is returned (as soon as the method is returned, the thread begins to notify additional events for the new UI).

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          Right. Well, I've seen "wait conditions":http://doc.trolltech.com/latest/qwaitcondition.html being used in a similar manner to your processed flag. The advantage of using wait conditions is that you won't have a busy wait loop. It'll be a bit nicer/cleaner than a simple boolean and as you noticed in your problem statement, QMutex probably won't work given the thread thingy.

          If there's only one GUI item that displays these messages, you could consider creating it beforehand and showing it on receiving this event. Then you don't have to wait for completion.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • A Offline
            A Offline
            augiekim
            wrote on last edited by
            #5

            Thank you Franzk.
            QWaitCondition is really suitable for me.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved