PDA

View Full Version : QProcess finishes imediately (it shouldn't)



Yes
10th September 2012, 15:52
I'm running Windows XP and I'm using Qt Creator, and here is the code I use to create the process and to log what is happening:



QProcess process;
cout << "Starting process..." << endl;
process.start("ping 1.1.1.1 -n 1 -w 3000 > nul");
process.waitForStarted();
cout << "Process started" << endl;
process.waitForFinished();
cout << "Process finished" << endl;

The command line "ping 1.1.1.1 -n 1 -w 3000 > nul" is supposed to make the program halt for 3 seconds (3000 milliseconds) between two pings before it times out, but the text "Process finished" is written to cout almost simultaneously as the other two lines of text, sometimes seemingly at the same time. If the command line is entered directly into the terminal (MS Command Prompt) instead, the command runs for three seconds before the terminal takes in keyboard input again, so it is not the command line that is wrong. So why isn't this working in Qt?

wysota
11th September 2012, 00:22
QProcess is not running a shell thus things like ">" will not work. It doesn't make sense to use it here anyway since QProcess is capturing all the output of the slave process.

Yes
11th September 2012, 09:13
Okay, that seems reasonable. I started another program instead which also waits for a centain amount of time, and that worked fine. Thanks!