From 0ebf8b095aee57c3b499b0ea879e7ed11c0b2009 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 6 Dec 2024 11:48:21 +0100 Subject: [PATCH] getCommandResult(): reset result if necessary (for instance for showing current result in GUI. --- UpdatePTUDevCtrl/process/command.cpp | 17 ++++++++++++++--- UpdatePTUDevCtrl/process/command.h | 6 ++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/UpdatePTUDevCtrl/process/command.cpp b/UpdatePTUDevCtrl/process/command.cpp index 8e0cf68..ab8f40d 100644 --- a/UpdatePTUDevCtrl/process/command.cpp +++ b/UpdatePTUDevCtrl/process/command.cpp @@ -5,6 +5,7 @@ #include #include #include +#include Command::Command(QString const &command, int start_timeout, int finish_timeout) : m_command(command.trimmed()) @@ -14,14 +15,23 @@ Command::Command(QString const &command, int start_timeout, int finish_timeout) , m_exitCode(-1) { } -QString Command::getCommandResult() const { - return m_commandResult; +QString Command::getCommandResult(bool reset) const { + QMutexLocker locker(&m_mtx); + + if (reset == false) { + return m_commandResult; + } + + QString commandResult = m_commandResult; + m_commandResult.clear(); + + return commandResult; } void Command::readyReadStandardOutput() { + QMutexLocker locker(&m_mtx); QProcess *p = (QProcess *)sender(); m_commandResult += p->readAllStandardOutput(); - // qCritical() << m_commandResult; } void Command::readyReadStandardError() { @@ -35,6 +45,7 @@ void Command::finished(int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) { // read all remaining data sent to the process, just in case QString d = p->readAllStandardOutput(); if (!d.isEmpty()) { + QMutexLocker locker(&m_mtx); m_commandResult += d; } disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardOutput())); diff --git a/UpdatePTUDevCtrl/process/command.h b/UpdatePTUDevCtrl/process/command.h index bc796ee..460f935 100644 --- a/UpdatePTUDevCtrl/process/command.h +++ b/UpdatePTUDevCtrl/process/command.h @@ -7,22 +7,24 @@ #include #include #include +#include class Command : public QObject { Q_OBJECT QString m_command; - QString m_commandResult; + mutable QString m_commandResult; int m_waitForStartTimeout; int m_waitForFinishTimeout; int m_exitCode; + mutable QMutex m_mtx; public: explicit Command(QString const &command, int start_timeout = 100000, int finish_timeout = 100000); - QString getCommandResult() const; + QString getCommandResult(bool reset = false) const; QString command() const { return m_command; } bool execute(QString workingDirectory, QStringList args = QStringList());