RedPanda-CPP/RedPandaIDE/compiler/executablerunner.cpp

86 lines
2.6 KiB
C++
Raw Normal View History

#include "executablerunner.h"
#include <QProcess>
#include <windows.h>
#include <QDebug>
2021-06-25 12:40:11 +08:00
#include "compilermanager.h"
ExecutableRunner::ExecutableRunner(const QString &filename, const QString &arguments, const QString &workDir):
QThread(),
mFilename(filename),
mArguments(arguments),
mWorkDir(workDir),
mStop(false)
{
}
void ExecutableRunner::stop()
{
mStop = true;
}
void ExecutableRunner::run()
{
emit started();
QProcess process;
mStop = false;
2021-06-25 12:40:11 +08:00
bool errorOccurred = false;
process.setProgram(mFilename);
process.setArguments(QProcess::splitCommand(mArguments));
process.setWorkingDirectory(mWorkDir);
process.setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments * args){
args->flags |= CREATE_NEW_CONSOLE;
args->startupInfo -> dwFlags &= ~STARTF_USESTDHANDLES;
});
2021-06-25 12:40:11 +08:00
process.connect(&process, &QProcess::errorOccurred,
[&](){
errorOccurred= true;
});
// qDebug() << mFilename;
// qDebug() << QProcess::splitCommand(mArguments);
process.start();
process.closeWriteChannel();
process.waitForStarted(5000);
while (true) {
process.waitForFinished(1000);
if (process.state()!=QProcess::Running) {
break;
}
if (mStop) {
2021-08-01 23:24:37 +08:00
process.closeReadChannel(QProcess::StandardOutput);
process.closeReadChannel(QProcess::StandardError);
process.closeWriteChannel();
2021-06-25 12:40:11 +08:00
process.terminate();
2021-08-01 23:24:37 +08:00
process.kill();
break;
2021-06-25 12:40:11 +08:00
}
if (errorOccurred)
break;
}
if (errorOccurred) {
switch (process.error()) {
case QProcess::FailedToStart:
emit runErrorOccurred(tr("The runner process failed to start."));
break;
case QProcess::Crashed:
if (!mStop)
emit runErrorOccurred(tr("The runner process crashed after starting successfully."));
break;
case QProcess::Timedout:
emit runErrorOccurred(tr("The last waitFor...() function timed out."));
break;
case QProcess::WriteError:
emit runErrorOccurred(tr("An error occurred when attempting to write to the runner process."));
break;
case QProcess::ReadError:
emit runErrorOccurred(tr("An error occurred when attempting to read from the runner process."));
break;
2021-06-25 12:40:11 +08:00
default:
emit runErrorOccurred(tr("An unknown error occurred."));
}
}
emit terminated();
}