1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "thread_startup.h"
25 #include <MUtils/Global.h>
29 #include <QElapsedTimer>
32 //-------------------------------------
34 //-------------------------------------
36 StarupThread::StarupThread(void)
42 StarupThread::~StarupThread(void)
46 //-------------------------------------
48 //-------------------------------------
50 void StarupThread::run(void)
54 runChecked1(this, m_success, &m_exception);
57 void StarupThread::runChecked1(StarupThread *const thread, volatile int &success, volatile bool *exception)
61 return runChecked2(thread, success, exception);
66 qWarning("Unhandled exception error in startup thread !!!");
70 void StarupThread::runChecked2(StarupThread *const thread, volatile int &success, volatile bool *exception)
74 success = thread->threadMain();
79 qWarning("Startup thread raised an C++ exception!");
83 //-------------------------------------
85 //-------------------------------------
87 QStringList StarupThread::runProcess(const QString &exePath, const QStringList &arguments, const QStringList *const extraPaths)
92 const QString fileName = QFileInfo(exePath).fileName().toUpper();
94 //Setup process object
95 MUtils::init_process(process, QDir::tempPath(), true, extraPaths);
97 //Try to start process
98 process.start(exePath, arguments);
99 if (!process.waitForStarted())
101 qWarning("Failed to launch %s -> %s", MUTILS_UTF8(fileName), MUTILS_UTF8(process.errorString()));
102 return QStringList();
109 //Wait until process has finished
110 QStringList processOutput;
111 while (process.state() != QProcess::NotRunning)
113 process.waitForReadyRead(1250);
114 while (process.canReadLine())
116 const QString line = QString::fromUtf8(process.readLine()).simplified();
119 processOutput << line;
122 if ((process.state() != QProcess::NotRunning) && timer.hasExpired(15000))
124 process.waitForFinished(125);
125 if (process.state() != QProcess::NotRunning)
127 qWarning("%s process encountered a deadlock -> aborting now!", MUTILS_UTF8(fileName));
133 QThread::yieldCurrentThread(); /*yield*/
137 //Make sure process has terminated!
138 process.waitForFinished(1250);
139 if (process.state() != QProcess::NotRunning)
141 qWarning("%s process still running, going to kill it!", MUTILS_UTF8(fileName));
143 process.waitForFinished(-1);
147 while (process.bytesAvailable() > 0)
149 const QString line = QString::fromUtf8(process.readLine()).simplified();
152 processOutput << line;
157 if (process.exitCode() != 0)
159 qWarning("%s failed with code 0x%08X -> discarding all output!", MUTILS_UTF8(fileName), process.exitCode());
160 return QStringList();
163 return processOutput;