Results 1 to 3 of 3

Thread: Problem with QProcess in two separate threads

  1. #1
    Join Date
    Feb 2010
    Location
    Finland
    Posts
    7
    Thanks
    1
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question 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:
    Qt Code:
    1. openMe open1;
    2. openMe open2;
    3. .
    4. .
    5. .
    6. .
    7. void class::someFunc(){
    8. open1.setFname(fname1);
    9. open1.start();
    10. open2.setFname(fname2):
    11. open2.start();
    12. }
    To copy to clipboard, switch view to plain text mode 

    openMe class looks like this:
    Qt Code:
    1. QString fname;
    2. openMe::openMe()
    3. {
    4. fname="";
    5. }
    6. void openMe::run(){
    7. fname.replace("/","\\\\");
    8. p.start("cmd.exe", QStringList() << /c << fname);
    9. p.waitForFinished();
    10.  
    11. }
    12.  
    13. void openMe::setFname(QString name){
    14. fname=name;
    15. }
    To copy to clipboard, switch view to plain text mode 

    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-

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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:

    Qt Code:
    1. void someFunc() {
    2. QProcess::startDetached("cmd.exe", QStringList() << /c << fname1); // what is /c anyway?
    3. QProcess::startDetached("cmd.exe", QStringList() << /c << fname2);
    4. }
    To copy to clipboard, switch view to plain text mode 
    No extra threads, no waiting for anything to finish.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    viheaho (18th March 2010)

  4. #3
    Join Date
    Feb 2010
    Location
    Finland
    Posts
    7
    Thanks
    1
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

Similar Threads

  1. Replies: 10
    Last Post: 14th January 2010, 23:55
  2. Replies: 1
    Last Post: 7th December 2009, 07:26
  3. Problem with threads
    By kvnlinux in forum Newbie
    Replies: 7
    Last Post: 21st August 2009, 17:29
  4. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 08:55
  5. Threads problem
    By boss_bhat in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2006, 15:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.