考虑以下代码(适用于Windows平台):
// 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");
}
在执行“start file.txt”之前,我怎么能等到“Sanity cmds”完成(最好是超时),并知道它的退出状态?如果前者失败,则不能执行后者。
我已经尝试过不同的方法,还有两个额外的考虑因素:
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.