Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Starting and terminating processes needed by my application

    General and Desktop
    2
    6
    2292
    Loading More Posts
    • 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.
    • L
      Luc4 last edited by

      Hi! I'm working on an application which needs two other processes to work. The processes must be started and must be terminated when my application starts and quits. These processes may take some time to start, so I would also like to load in a detached thread to prevent my interface from appearing after some seconds:

      @mythread = new QThread();
      QObject::connect(&a, SIGNAL(aboutToQuit()), ProcessHolder::getInstance(), SLOT(mydestroy()));
      QObject::connect(&a, SIGNAL(aboutToQuit()), mythread, SLOT(quit()));
      QObject::connect(mythread, SIGNAL(finished()), mythread, SLOT(deleteLater()));
      ProcessHolder::getInstance()->moveToThread(mythread);
      mythread->start();
      QMetaObject::invokeMethod((FS_ProcessHolder::getInstance()), "init");@

      the mydestroy() method terminates the processes and deleteLater() the object.
      This works correctly when I start my application, wait for the processes to start successfully, then quit my application. The processes are correctly terminated.
      If I start my application and immediately close it, before the processes are completely started, my mydestroy() method is not even invoked. Maybe because the thread owning the object is still busy when mydestroy() is invoked?
      Any idea what I can do to fix this?
      Thanks!

      1 Reply Last reply Reply Quote 0
      • ?
        Guest last edited by

        What about catching the quit signal in a specific method? This way you could delay main app's quit process long enough for the other processes to finish loading.

        (like, using sigaction, SIGQUIT?, sleep...)

        --

        1 Reply Last reply Reply Quote 0
        • L
          Luc4 last edited by

          So I should put something like a sleep in the signal handler to give needed time to the other threads to finish the cleanup process?
          Thanks!

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

            There is no need at all to use threads to start external processes. QProcess will suffice, and is asynchronic by itself already. Simply don't use the synchronous (waitFor...) API, and you have an async setup.

            1 Reply Last reply Reply Quote 0
            • L
              Luc4 last edited by

              I know that. But I also do other initializations, which are not asynchronous, so that solution seems pretty good. And the initialization doesn't suffer issues of any kind at the moment. My problem is related to the cleanup. Why isn't that method invoked? I need to invoke that method to terminate correctly my processes and cleanup. This way instead, my processes remain running.
              Thanks!

              1 Reply Last reply Reply Quote 0
              • L
                Luc4 last edited by

                At the end, I came up with a solution which seems to work for the moment. The problem I suppose was related to the fact that the application was terminated before the mydestroy() method ended.
                This executes the method in the same thread and seems to work better:

                @QObject::connect(&a, SIGNAL(aboutToQuit()), ProcessHolder::getInstance(), SLOT(mydestroy()), Qt:DirectConnection);@

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post