Problem with QProcess in two separate threads
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:
Code:
openMe open1;
openMe open2;
.
.
.
.
void class::someFunc(){
open1.setFname(fname1);
open1.start();
open2.setFname(fname2):
open2.start();
}
openMe class looks like this:
Code:
openMe::openMe()
{
fname="";
}
void openMe::run(){
fname.replace("/","\\\\");
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-
Re: Problem with QProcess in two separate threads
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:
No extra threads, no waiting for anything to finish.
Re: Problem with QProcess in two separate threads
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.