0

This is a code snippet for a QNX target. It works fine when I run it on system and when I do the next ignition cycle/ restarting my system, the GUI is freezes/hangs.

If possible, please tell me what is wrong in this code.

I tried with readAllStandardOutput and finished and started signal too with same issue. It did not help.

qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<__PRETTY_FUNCTION__<<!usbProcess;
usbProcess = new QProcess();
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<usbProcess->pid();
usbProcess->start("usb");
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<usbProcess->pid();;
usbProcess->waitForReadyRead();
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__;
usbProcess->waitForFinished();
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__;
text =  usbProcess->readAll();
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<!usbProcess<<text;
usbProcess->closeReadChannel(QProcess::ProcessChannel::StandardOutput);
usbProcess->closeReadChannel(QProcess::ProcessChannel::StandardError);
usbProcess->closeWriteChannel();
usbProcess->close();
delete usbProcess;
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<!usbProcess;
Arun Kumar
  • 151
  • 3
  • 10
  • I'm not sure I completely understand the problem but... if you check the [QProcess documentation](http://doc.qt.io/qt-5/qprocess.html), both `waitForReadyRead` and `waitForFinished` come with the warning "Calling this function from the main (GUI) thread might cause your user interface to freeze". Use signals/slots instead. – G.M. Sep 14 '17 at 10:29
  • I tried. Please have a look... – Arun Kumar Sep 14 '17 at 10:58
  • usbProcess = new QProcess( this ); QObject::connect(usbProcess, &QProcess::readyReadStandardOutput, [this](){ text = usbProcess->readAllStandardOutput(); text.replace(",", ",\n"); text.replace(":", "\n:"); qDebug()<<__LINE__<<__PRETTY_FUNCTION__<start("usb"); – Arun Kumar Sep 14 '17 at 10:59
  • Either use a dedicated thread or use the non-blocking API with signals or slots. Your code doesn't do neither. – dtech Sep 14 '17 at 17:38
  • I try std:system("usb"). it works but not with QProcess. I unable to figure out what is the problem in with my code. – Arun Kumar Sep 18 '17 at 06:09

1 Answers1

1

You are running the blocking process on the GUI thread. That is why your GUI is hanging.

I usually create two methods. One which is Blocking and one which is Non-Blocking. The Blocking method is invoked using the Qt Concurrent framework by the Non-Blocking method. A signal is emitted when finished and returns any data.

// Runs the usb process without blocking
void MyClass::runUsbProcess(){
    QtConcurrent::run(this, &MyClass::runUsbProcessBlocking);
}

// Runs the usb process while blocking
void MyClass::runUsbProcessBlocking(){
    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<__PRETTY_FUNCTION__<<!usbProcess;
    usbProcess = new QProcess();

    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<usbProcess->pid();
    usbProcess->start("usb");

    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<usbProcess->pid();;
    usbProcess->waitForReadyRead();

    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__;
    usbProcess->waitForFinished();

    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__;
    text =  usbProcess->readAll();
    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<!usbProcess<<text;

    usbProcess->closeReadChannel(QProcess::ProcessChannel::StandardOutput);
    usbProcess->closeReadChannel(QProcess::ProcessChannel::StandardError);
    usbProcess->closeWriteChannel();
    usbProcess->close();

    delete usbProcess;
    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<!usbProcess;

    emit usbProcessFinished(text);
}
konakid
  • 65
  • 1
  • 9