Search code examples
c++qtqtcoreqprocess

How to stop a detached process in qt?


After starting a process with QProcess::startDetached, how can I stop it later?

Say the main program runs, then starts the detached process, which runs independently. The user closes the main program, then later opens it up again and wants to stop the process. How would I find the process and then stop it?


Solution

  • Is there a way I could prevent the application from the same process twice?

    No, it will be decoupled from your application. You could get the the PID of it and then send a SIGSTOP on Linux, but this is platform specific and will not work without POSIX support, like with msvc. You would need to hand-craft your version therein.

    Is there a way I could prevent the application from the same process twice?

    Yes, by using lock file in the detached process. If that detached process happens to be written in at least partially Qt, you could use the QLockFile class.

    If you happen to detach some platform specific process, then you have the same recurring issue again, for sure.