Consider the following code (for Windows platforms):
// Function called lots of times.
void showText(QString str, QString filename, bool open = false)
{
static QProcess* myProcess = new QProcess();
if (myProcess->state() == QProcess::NoRunning)
myProcess->start("cmd.exe");
<Code that creates "filename" and writes "str" in it>
myProcess->write("Sanity cmds\r\n");
<Wait to "Sanity cmds" to finish and check status
before continuing>
if (open) // True only sometimes.
myProcess->write("start file.txt");
}
How can I wait till "Sanity cmds" has finished (preferably with timeout), and know its exit status, before executing "start file.txt"? If the former fails, the later must not be executed.
I've tested with different approaches and there's two additional considerations:
A) The "cmd.exe" process cannot be killed to open a new instance.
B) If "Sanity cmds" returns something to its standard output or error, such output cannot be discarded.