Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

The true meaning of the warning for QProcess::waitForFinished



  • Hi, all.

    I am using Qt 5.6.3 with vs2013 x64 and am using QProcess::waitForFinished function in a slot function on GUI application.

    Last week, I have watched infinite freeze on my application on Windows 10 once.
    I have tried to launch "Task Manager" with CTRL+ALT+DELETE however there was no response on Windows also.
    Only the mouse cursor (WAITING - ROUNDED - CURSOR) could move.
    That's why I am re-checking the document.

    In the document, there is a warning message that said "Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze." however I could not understand the true meaning because of the word "freeze".
    https://doc.qt.io/qt-5/qprocess.html#waitForFinished

    If the meaning is "Infinite freeze", I can't use the function for my internal design. Because the implementation is wrong.
    If the meaning is "a few milliseconds or until finished", I can use the function for my internal design. Because it's just a behavior of the function.

    If you know the true meaning, please let me know.

    Shin


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The meaning is: until the process finishes. Which might go from a few millisecond to infinite if the application called through QProcess doesn't end.



  • Hi SGaist-san,

    Thank you for your clear explanation.
    I understood that using waitForFinished in the slot function is not a wrong implementation.

    Thank you for your quick reply.

    Shin


  • Lifetime Qt Champion

    @Shinichiro said in The true meaning of the warning for QProcess::waitForFinished:

    I understood that using waitForFinished in the slot function is not a wrong implementation.

    Just to make it clear for the future reader: waitForFinished() should not be used in a Slot. It should only be used in a separate thread,where you don't use Signals&Slots.

    Regards



  • Hi, aha_1980-san,

    Thank you for pointing out.
    Okay, we can't use waitForFinished() in a slot function, however why we can't use it in a slot?
    The function has an argument for the time out, so the function will not be in a finite loop, right?
    I think I should understand the internal design of the GUI thread more to have a true understanding.

    Regards,
    Shin


  • Qt Champions 2019

    @Shinichiro What is the point to use signals/slots and waitFor*() functions at the same time? If you call waitForFinished() in a slot it will block the slot until it is finished or times out - until then the thread where your slot is executed will be blocked (if this thread is UI thread your UI will be blocked). The purpose of signals/slots IS to avoid blocking the event loop, so if you already use them why do you need waitForFinished?



  • @jsulm Hi, I was re-using an instance of QProcess in a class for the next processing. so I was using waitForFinished to wait for the previous task to end. That's why I was using the interface. What's the better implementation for that?


  • Lifetime Qt Champion

    @Shinichiro said in The true meaning of the warning for QProcess::waitForFinished:

    What's the better implementation for that?

    Well, connect a slot to the finished() signal.



  • @Shinichiro
    What @aha_1980, @jsulm are trying to guide you towards is: instead of writing your code to wait for things to happen (QProcess finishing) and then doing something next, write it so there is no waiting and instead do your "something next" when a signal arrives to tell you to proceed. That way your GUI does not "block"/"freeze".



  • Hi, Thank you for your reply.
    I totally changed my implementation using the signal and slots.
    Thank you so much again.