PDA

View Full Version : Problem with QProcess in two separate threads



viheaho
18th March 2010, 21:56
Hi!
I'm working on program where two types of documents are created and afterwards opened. For opening I'm using QProcess in a thread. The problem is that when try to open files it only opens the last one twice.
Here are parts of code.
Main program:


openMe open1;
openMe open2;
.
.
.
.
void class::someFunc(){
open1.setFname(fname1);
open1.start();
open2.setFname(fname2):
open2.start();
}


openMe class looks like this:


QString fname;
openMe::openMe()
{
fname="";
}
void openMe::run(){
fname.replace("/","\\\\");
QProcess p;
p.start("cmd.exe", QStringList() << /c << fname);
p.waitForFinished();

}

void openMe::setFname(QString name){
fname=name;
}


As I mentioned earlier opening files separately works great but when running simultaneously it only opens last file twice. Is there some obvious issue or expected behaviour?

-Ville-

wysota
18th March 2010, 22:07
You are operating on a global variable so if you change it from within one thread, the other sees it changed too. QThread::start() only schedules the thread, it doesn't start it immediately.

In general I completely fail to understand why you are starting those processes in separate threads and waiting for them to finish. The code you have written is more or less equivalent to:


void someFunc() {
QProcess::startDetached("cmd.exe", QStringList() << /c << fname1); // what is /c anyway?
QProcess::startDetached("cmd.exe", QStringList() << /c << fname2);
}
No extra threads, no waiting for anything to finish.

viheaho
18th March 2010, 22:52
Thank you for your reply.
There are two major reasons why I have done it so. First of all it's a GUI application and opening a file takes lot of time. If it wouldn't be threaded it would freeze the application for a while. The second reason is related to first one, if I don't wait until file is opened it exits the function and processes quit before they even started.
Method that you suggest works great. I've fordotted to put quotes around the "/c" parameter. It just closes shell windows after closing files.