PDA

View Full Version : I start a external fortram program, how to terminate?



dolphins
28th October 2007, 11:48
i have created MyThread, subclass QThread and reimplement run(),
class MyThread : public QThread
{
Q_OBJECT
public:
void run();
void stop();
private:
QProcess *MyProcess;
};

MyThread::MyThread(QWidget *parent )
{
MyProcess = new QProcess(parent);
}

void MyThread::run()
{
MyProcess->execute("ComputerFortran"); //call for Fortran computer program
}

void MyThread::stop()
{
MyProcess->kill(); // or
// MyProcess->terminate();
}
when i start external fortran program ,i call start() function.

Quote:

class ...............
{...........
MyThread thread;
............
};

.....................
thread.start();

but I call thread.stop(), the fortran program didn't kill? how to terminate??

jpn
28th October 2007, 12:01
You don't need a thread to run QProcess. It only makes things unnecessarily more complex. QProcess runs an external process, which doesn't block the main application anyhow unless you call one of the QProcess::wait*() methods (or QProcess::execute() which waits the process to finish). Instead, just start it and connect to its signals to get notified when something interesting happens:


process = new QProcess(this);
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processOutput(int, QProcess::ExitStatus)));
process->start("ComputerFortran");

void MyClass::processError(QProcess::ProcessError error)
{
...
}

void MyClass::processOutput(int exitCode, QProcess::ExitStatus exitStatus)
{
....
}

dolphins
28th October 2007, 12:21
Thanks Jpn !!

the external fortran program will take several hours on outputting 100 data files, I think I

will kill the fortran program when I have found errors in first 10 data files, that's to say, the

fortran program process have not finish, how to terminate the fortran process?


By the way, I want to know how to display terminal characters in QLabal or QEditin in real time ?

jpn
28th October 2007, 13:07
the external fortran program will take several hours on outputting 100 data files, I think I

will kill the fortran program when I have found errors in first 10 data files, that's to say, the

fortran program process have not finish, how to terminate the fortran process?

Since terminate() has no effect, you might have to kill() it. Earlier the situation was kind of problematic since you were commanding the process from multiple threads but it should work fine after you do the proposed changes.


By the way, I want to know how to display terminal characters in QLabal or QEditin in real time ?
QProcess emits readyReadStandardError() and/or readyReadStandardOutput() when there is something available to read.