Solved Manage Qthread -resource creation and deletion
-
Ok thanks,..i will try it out
-
i tried with asynchronous processes approach , but with that please help me with how to handle the write and wait appropriately.
With the below approach i can able to run only one Qprocess, remaining gets struct.for(int i=0;i<8;i++) { stream << "started thread:" << i; process = new QProcess(); process->setWorkingDirectory("C:\\Users\\H336803\\Documents\\Visual Studio 2005\\Projects\\QthreadTest\\QthreadTest\\output\\"+QString::number(i)); process->setObjectName("Loadcase"+QString::number(i)); connect(process,SIGNAL(started()),this,SLOT(processStarted())); connect(process,SIGNAL(finished(int)),this,SLOT(processFinished(int))); connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater())); connect(process,SIGNAL(error()),this,SLOT(processError())); process->start("felix2.exe", QIODevice::ReadWrite|QIODevice::Append); } void QthreadTest::processStarted() { QMutex mutex; QString curr = process->objectName(); stream << ", Process name" << curr; connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(readInput())); } void QthreadTest::readInput() { QString str = process->readAllStandardOutput(); if(str.contains("Main Menu")) { process->write("3\n"); connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(waitforWriting())); } } void QthreadTest::waitforWriting() { process->write("LoadCase1.felix\n"); process->write("\n"); connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(readInput1())); } void QthreadTest::readInput1() { QString str = process->readAllStandardOutput(); if(str.contains("Main Menu")) { process->write("5\n"); connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(processFin())); } }
-
@meganathan said in Manage Qthread -resource creation and deletion:
readyReadStandardOutput
Why do you connect this signal to three different slots? All of them will be executed. You can handle everything with one slot, or disconnect slots you don't need anymore.
-
since can't use waitforReadReady() which blocks every qprocess
-
Hi,
An additional note, in your loop you are overwriting the same variable name process over and over again. This is not a good idea since you re-use it later on.
-
How can i use asynchronous Interactive QProcess withour waitforReadyRead(-1)? .Please help me how i can do it
-
@meganathan said in Manage Qthread -resource creation and deletion:
How can i use asynchronous Interactive QProcess withour waitforReadyRead(-1)?
Using signals slots, but don't connect 3 different slots to same signal as I stated above.
-
You don't need to wait for writing, std i/o is buffered so if you don't care about the state you can write multiple things to the process at once and the process will receive them and use them in order without you needing anything else
-
i made the changes as requested, but am i facing problem to identify which process is started and which process has emitted readReadyStandardOutput().
void QthreadTest::startThread()
{
processFinish =0;
for(int i=0;i<8;i++)
{
stm << "started thread:" << i;
process = new QProcess();
listOfProcess->append(process);
}
startProcesses();
}void QthreadTest::startProcesses()
{
for(int i=0;i<listOfProcess->size();i++)
{
listOfProcess[i]->setWorkingDirectory("C:\Users\H336803\Documents\Visual Studio 2005\Projects\QthreadTest\QthreadTest\output\"+QString::number(i));
listOfProcess[i]->setObjectName("Loadcase"+QString::number(i));
connect(listOfProcess[i],SIGNAL(started()),this,SLOT(processStarted()));
connect(listOfProcess[i],SIGNAL(finished(int,QProcess::ExitStatus exitStatus)),this,SLOT(processFinished(int,QProcess::ExitStatus)));
connect(listOfProcess[i],SIGNAL(readyReadStandardOutput()),this,SLOT(readInput()));
connect(listOfProcess[i],SIGNAL(error()),this,SLOT(processError()));listOfProcess[i]->start("felix2.exe", QIODevice::ReadWrite); }
}
void QthreadTest::readInput()
{
QString str = process->readAllStandardOutput(); // need help hereif(str.contains("Main Menu")) { process->write("3\n"); } else if(str.contains("Enter")) { process->write("LoadCase1.felix\n"); process->write("\n"); } else if(str.contains("Results")) { process->write("5\n"); }
}
As you can see in readInput() function , i coan't get which Qprocess is currently emitted readReadyStandardOuput() signal,
Please help what i am doing wrong? -
@meganathan You can use new connect syntax and lambdas, like:
connect(listOfProcess[i],&QPRocess::started, [listOfProcess[i], this] { ... });
-
@jsulm i am using Qt 4.8.5 ,this syntax won't work i guess
-
@meganathan said in Manage Qthread -resource creation and deletion:
this syntax won't work i guess
True
Then take a look at https://doc.qt.io/qt-5/qsignalmapper.html -
Thanks this works