Program hangs
-
I have an issue where my program appears to hang. The code below gets a filename from the user and builds a string that is passed to QProcess. A progress bar is started to give the user a visual clue that the process is running. The progress bar is hidden when the process completes.
The issue is that very rarely the progress bar will max out and no completion is sent back to the user. The program just sits. This doesn't happen often, out of about 2000 users, I've gotten a half-dozen sporadic reports. And with those reporting, it's not a consistent problem. It has never happened to me personally.
If you see anything in the code that could be causing this I'd appreciate it if you could point it out. If you have a solution that you can show in code it would be very helpful.
Thanks!
@
QString RunProcess(QString cstring)
{
QProcess run_command;
run_command.setProcessChannelMode(QProcess::MergedChannels);
run_command.start(cstring);run_command.waitForStarted();
while(run_command.state() != QProcess::NotRunning)
qApp->processEvents();QString command=run_command.readAll();
return command;
}void MainWindow::on_sideload_Button_clicked()
{if (!isConnected) { QMessageBox::critical( this, tr("adbFire"), tr("Device not connected")); return; } QElapsedTimer rtimer; int nMilliseconds; rtimer.start();
QString fileName = QFileDialog::getOpenFileName(this,
tr("Select app to install"), sldir , tr("APK Files (*.apk)"));if (!fileName.isEmpty() ) { QFileInfo finfo(fileName); sldir = finfo.absolutePath(); QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "Install", "Install "+fileName+"?\n", QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { ui->progressBar->setHidden(false); ui->progressBar->setValue(0); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(TimerEvent())); timer->start(tsvalue); QString cstring = adb + " install -r " + '"'+ fileName+'"'; QString command=RunProcess(cstring); ui->progressBar->setHidden(true); logfile(cstring); logfile(command); nMilliseconds = rtimer.elapsed(); logfile("process time duration: "+ QString::number(nMilliseconds/1000)+ " seconds" ); if (command.contains("Success")) QMessageBox::information(this,"","Installed"); else QMessageBox::critical(this,"","Install failed"); }
}
}
@
-
Well I'm not sure but I think it most likely has something to do with the while loop inside RunProcess:
@while(run_command.state() != QProcess::NotRunning)
qApp->processEvents();@
Maybe the process you're running doesn't cause Qt to emit a NotRunning signal? Or maybe it emits another signal when it finishes, like an idle signal (this is all speculation).Maybe it would be safer to use:
@while(run_command.state() == QProcess::Running)
qApp->processEvents();@
That way it takes into account any other finished states the process might have.But that's just a guess. Hope it helps.
-
Hi,
You should check the return value from waitForStarted to see whether the program really started