How to make synchronous function from a asynchronous function without QEventLoop



  • Hello,

    I'm pretty new to Qt and I would like to synchronously wait for an asynchronous function. So I used a QEventLoop object with its quit() slot connected to a finished() signal. But according to some people this should not be use in a "real app" even with a QTimer to prevent dead lock (in case of the signal is emitted before entering the function QEventLoop::exec() ).
    I don't want to write an active loop that will check a flag's state cause it will consume all the CPU time.

    Most of the examples found on the web use a QEventLoop object.

    So how can I make to wait for the end of an asynchronous function without consuming all of the CPU time, without using a QEventLoop object and without block the GUI ?

    Thanks
    Alex



  • Why don't you put your function call in a QThread



  • Cause I will have to wait for the thread to achieve the tasks, which is the same problem. I will have to wait in the main thread.

    For example, I have to initialize a device connected using a bluetooth link. So I have to discover which bluetooth services are available (which is made asynchronously), connect to a bluetooth socket (which is made asynchronously) and then I can send and receive data to/from the device.
    So I must wait for all of these initialization steps to finish.

    I don't want to use several slots to do so if I can simply do, which is much more convenient.
    @if( ptDevice->open() )
    {
    // do some stuff
    }
    @

    What the point of dispatching it into several slots if we can do it in a single function ? Sometimes iIt complicates the code for no reasons.
    Some tasks have to be done synchronously.





  • Thanks guys for your replies and your time.

    But I won't create a thread for each task than can be done synchronously that make no sense. My app will have hundred of threads or hundreds of slots for no good reasons. Debugging it will turn into a nightmare.

    I'll find another solution, I don't believe it can't be done with Qt. It can be done with Delphi, Borland C++ and Visual C++.

    Thanks.



  • Assume you have two objects with a start() function and finished() signal. You want to start() the second object once you have received the finished() signal from the first object. Below is some pseudo code.

    @
    void MyClass::setup() {
    A *a = new A();
    B *b = new B();
    connect(a, SIGNAL(finished()), this, SLOT(startB()));
    connect(b, SIGNAL(finished()), this, SLOT(chainComplete()));
    }

    void MyClass::runChain() {
    a->start();
    }

    void MyClass::startB() {
    b->start();
    }

    void MyClass::chainComplete() {
    qDebug() << "Chain has completed";
    }
    @

    You can extend this pattern until you have reached the desired chain. Essentially the next step is executed once the end signal from the previous object was received.


  • Lifetime Qt Champion

    Wouldn't then a state machine be more clean ?



  • By all means, you can do that too. I just wanted to demo the main principle.

    A state machine becomes more efficient the longer the chain is.



  • [quote author="Alex Blasche" date="1404976000"]You can extend this pattern until you have reached the desired chain. Essentially the next step is executed once the end signal from the previous object was received.[/quote]
    Thanks Alex, I know how to chain slots and signals but if I do that in my case I'll have tons of slots, for no good reasons to me.

    [quote author="SGaist" date="1404978056"]Wouldn't then a state machine be more clean ?[/quote]
    Definitely.



  • [quote author="XelaG" date="1404974539"]
    I'll find another solution, I don't believe it can't be done with Qt. It can be done with Delphi, Borland C++ and Visual C++.
    [/quote]

    I think there's some ambiguity about what is meant by an asynchronous function. Maybe with an example and an illustration of a solution in Delphi, Borland C++ or MS Visual C++, someone can translate it into something that works with Qt. Qt is after all largely a C++ framework.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.