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. Running QProcess in Thread to not freeze GUI on waitForFinished()
Forum Updated to NodeBB v4.3 + New Features

Running QProcess in Thread to not freeze GUI on waitForFinished()

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 14.8k Views 1 Watching
  • 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.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    I am having a little issue with my QProcess. I have an application that is basically and overlay to spawning other applications (aka, a window with buttons that once one of the buttons is clicked, a new executable is spawned).

    The only way I though of doing this was to start a QProcess when the button is clicked (to open the desired app). Unfortunately, I ran into the issue that the GUI keeps freezing when after I start the process and waitForFinished(-1).

    In turn, I chose the route of creating a QThread, with the QProcess in it, so when it runs, I can waitForFinished(-1) without freezing the GUI.

    Now I am running into more errors with this logic in which my QThread is throwing debug errors. Heres the code.

    This is the function that is a slot to the utility button on my main form.
    @
    void mainwindow::utils()
    {
    threadutils process;
    process.start();
    }
    @

    Heres the class file for the thread
    @
    #include "threadutils.h"

    threadutils::threadutils(QObject *parent) :
    QThread(parent)
    {
    program = "utils/converter";
    }

    void threadutils::run()
    {
    QProcess converter;
    converter.start(program);
    while(converter.waitForFinished(-1) == false){
    QCoreApplication::processEvents();
    }
    }
    @

    Is this the proper way to approach this? I have about 7 buttons on my main form and was planning to create a thread for each one, either that or just create one thread with a case statement and have a global pointing to which button was pushed. Either way, I feel like this should work.

    My build output crashes and says:
    QThread: Destroyed while thread is still running
    QMutex: mutex destroy failure: Device or resource busy

    I dont really understand this since I should be in a while loop...

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexisdm
      wrote on last edited by
      #2

      You allocated the threadutils object on the stack, so it is destroyed at the end of the utils() function. The QThread object should exist as long as the thread is running, so you should allocate it on the heap/dynamically.

      But anyway, you don't need to have a thread for each process, or even to subclass QThread to use it. You can look at the Worker class in "that article.":http://developer.qt.nokia.com/wiki/Threads_Events_QObjects#913fb94dd61f1a62fc809f8d842c3afa .

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

        Another approach would obviously be to simply not use the waitFor* methods, but instead use the relevant signals and slots. That way, you don't need any threads and you can still keep your application responsive.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vezprog
          wrote on last edited by
          #4

          I thank you for your responses. Andre, I dont think that will work since the applications that I am trying to spawn do not exist in the project itself, they are just separate executable's build on the system. Therefore, I have no way of communicating with the other applications using a protocol of my choice...since I cant modify code.

          I will look at alexisdm's way right now! Thank you!

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

            Yes, you can. QProcess supports signals that allow you to use it asynchronously.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #6

              Why not follow the Andre's suggestion without using threads pitfall ?
              Just connect your slot to "QProcess::finished":http://doc.qt.nokia.com/latest/qprocess.html#finished signal. You will be informed when the process ends + process exit code.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                Maybe using a [[Doc:QProgressDialog]] to inform the user that something's going on.

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

                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