0

My problem is that I am starting an executable in a QProcess like the following:

QProcess Work;
Work.start(program.exe);

This executable runs since it has been started in background and I can send requests to it. If I have finished I am closing the executable the following way:

Work.close();

But if I am looking at the Taskmanager the program.exe is running furthermore. I dont understand that behaviour because I thought the executable would been stopped if I close the Process.

Lehtim
  • 125
  • 1
  • 12
  • 1
    With close you just finish communication with process, you are not killing it – demonplus Sep 30 '16 at 06:27
  • What exactly *is* `program.exe` and how would you normally terminate it if running it at a command line? – G.M. Sep 30 '16 at 07:02
  • program.exe is a program which starts a parallel thread to communicate with another IP. The executable does have a command terminate and I think I will use these command because I have found out that killing a process is very bad. – Lehtim Sep 30 '16 at 11:19

2 Answers2

0

You should use void QProcess::terminate() or void QProcess::kill() for it.

terminate() - attempts to terminate the process.

kill() - kills process immediately.

Konstantin T.
  • 994
  • 8
  • 22
  • And even terminate() or kill() work only for some programs not for all – demonplus Sep 30 '16 at 06:27
  • 1
    in unix `kill()` send SIGKILL signal. This signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal. There are only a few rare cases, when SIGKILL is not working. – Konstantin T. Sep 30 '16 at 06:40
  • We don't know platform exactly – demonplus Sep 30 '16 at 06:43
  • the platform is windows 7 and I have also tried kill() but i have heard it isn't a good idea to kill() or terminate() it directly because any file that has been opened can't be closed by the application if we kill it immediatelly. – Lehtim Sep 30 '16 at 09:25
  • On Windows, terminate() posts a WM_CLOSE message to all top-level windows of the process and then to the main thread of the process itself. So program **can** save file or prompt the user for confirmation if it necessary. But of course it should be implemented by the program developer. – Konstantin T. Sep 30 '16 at 09:48
  • according to the documentation, close() already uses kill: `void QProcess::close(): Closes all communication with the process and kills it. ... ` – smerlin Oct 05 '16 at 16:16
0

I had a very similar case, although I was working on linux embedded with QT 4.8. Not sure if this can help you, but since I can't put it in a comment, I have to make a separate answer.

Do you set a parent to your QProcess? In my case, I instantiated QProcess like this

QProcess *p = new QProcess;

and I could see in the running processes list that each time I instantiated a new QProcess, I got a new process that couldn't be closed by close(), terminate() or kill(). When I finally rewrote the code like this

QProcess *p = new QProcess(mainW); //mainW was my GUI class, which handled also the closing of the process

the problem solved itself, I didn't even need to call any function to close the process. In my case I was sure the processes I called were finishing, since almost all of them were audio files, so I could hear them finishing. The others were processes that my program waited to complete, so again I was sure they ended because my program wasn't stuck waiting for them,

Hope this can help, despite the different OS.

il_boga
  • 1,305
  • 1
  • 13
  • 21