PDA

View Full Version : suspending a QProcess



qt_gotcha
13th August 2010, 12:11
I am trying to suspend a Qprocess and thought about using a function linked to a toolButton that toggles


if (pause)
calcProcess->waitForBytesWritten(-1);
else
{
char buf[8];
buf[0]='\0';
calcProcess->write(buf, 8);
}
I hoped waitForBytesWritten might suspend the Qproces until I write some bytes to it. The proces doesn't take any writing BTW, it only emits character.
Is there a way to do this?
I could implement the proces in a QThread but suspening the thread would not suspend the process, or would it?

tbscope
13th August 2010, 13:45
On a posix system, send the suspend signal (SIGTSTP) and continue signal (SIGCONT)

WaitForBytesWritten blocks till some data has been written to the process.

qt_gotcha
13th August 2010, 14:18
ok, how do I do that (new to Qt)?
Can I link this to a toolbutton

connect(toolButton_pauserun, SIGNAL(SIGTSTP),this, ????);

or
connect(toolButton_pauserun, SIGNAL(toggled(bool)), this, SLOT(suspendModel(bool)));
and in suspendModel

emit SIGNAL(SIGTSTP);

how do I the QProcess to receive this?
thanks

tbscope
13th August 2010, 14:46
No, that is not correct.

I'm talking about system signals. Not Qt signals. This is something completely different. And this is highly system specific. This won't work on Windows for example!

Use the following commands

kill -s SIGTSTP thePIDofYourProgram
kill -s SIGCONT thePIDofYourProgram

One thing I do need to say:
This technique shouldn't be used when you can build a pause option into the program being run by QProcess. But if the program being used is closed source and doesn't provide a pause option, you have no choice

Edit: And I'm sure data corruption can happen when using this technique, so be careful and read as much as you can about it.

qt_gotcha
13th August 2010, 15:10
thanks, but that is outside my scope
on the other hand the process information has a handle to the main thread of the process which can then be suspended (that's how I did it in borland). in windows:
typedef struct _PROCESS_INFORMATION {
HANDLE hProcess;
HANDLE hThread; <======
DWORD dwProcessId;
DWORD dwThreadId;
}

found it:

#include <windows.h>

void nutshellqt::suspendModel(bool pause)
{
PROCESS_INFORMATION *pi = calcProcess->pid();

if (pause)
{
SuspendThread(pi->hThread);
}
else
{
ResumeThread(pi->hThread);
}
}
of ocurse only windows, for unix something else would have to be implemented. but for now this will do

tbscope
13th August 2010, 15:22
If all you want to do is stop reading, you can also close the read channel. The process will still run in the background, but you will not receive any data.

qt_gotcha
13th August 2010, 15:33
sorry for editing my reply while you are answering.
I want the actual proces to suspend. In any case thanks for your help