0

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.
ABu
  • 10,423
  • 6
  • 52
  • 103
  • Is there some reason you can't just listen for the `finished()` signal? http://doc.qt.io/qt-5/qprocess.html#finished – MrEricSir Aug 01 '15 at 18:27
  • The finished signal is thrown when a process finishes. And cmd.exe doesn't finish in my case. What finishes is a command executed by cmd.exe process, not cmd.exe itself. – ABu Aug 01 '15 at 18:43

0 Answers0