Results 1 to 5 of 5

Thread: problem running GUI thread and worker thread concurrently

  1. #1
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default problem running GUI thread and worker thread concurrently

    I am using 2 threads: GUI thread and 1 worker thread(grabthread),but i am not able to run them concurrently.
    My main() function is like this :
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow win;
    5. win.show();
    6. GrabThread thread;
    7. thread.start();
    8. qDebug() << "hello from GUI thread " << a.thread()->currentThreadId();
    9. thread.wait(); // do not exit before the thread is completed!
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    and grabthread.h like this:
    Qt Code:
    1. class GrabThread : public QThread
    2. {
    3. Q_OBJECT
    4. private:
    5. void run();
    6. };
    To copy to clipboard, switch view to plain text mode 

    What is happening is that My GUI pops up only after grabthread has been fully executed and exits.
    I want my GUI and grabthread to start simultaneously so that i can use multithreading.

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem running GUI thread and worker thread concurrently

    Hi,

    Qt Code:
    1. ...
    2. thread.start();
    3. thread.wait();
    4. ...
    5. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    This code waits until the thread finishes to start the main application.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem running GUI thread and worker thread concurrently

    What is happening is that My GUI pops up only after grabthread has been fully executed and exits.
    I want my GUI and grabthread to start simultaneously so that i can use multithreading.
    In the QThread::run() function have you written exec() ? Untill you do that the event loop of your thread will not start. The default implementation of QThread run just has the exec.

    Also note that that for almost all the cases, QThread need not be inherited from thread. Refer the latest notification. This will make you life easy with one less inherited class to worry about in program.

  4. #4
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem running GUI thread and worker thread concurrently

    Quote Originally Posted by prkhr4u
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow win;
    5. win.show();
    6. GrabThread thread;
    7. thread.start();
    8. qDebug() << "hello from GUI thread " << a.thread()->currentThreadId();
    9. thread.wait(); // do not exit before the thread is completed!
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    well look at your thread.wait() function... it is blocking the event loop until the thread is done.
    So return a.exec() ( which enters the main event loop ) will only be executed after the thread is done , hence your GUI pop outs after the thread is done.

    Good Luck.

  5. The following user says thank you to toufic.dbouk for this useful post:

    prkhr4u (17th October 2013)

  6. #5
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem running GUI thread and worker thread concurrently

    @toufic.dbouk:
    I have removed the line thread.wait() and now the program is running just fine..thanks for the info
    @pkj:
    I have not written the exec() inside Grabthread::run()
    I will keep your advice in mind.Actually don't know about event loop..will study and tell..

Similar Threads

  1. QTimer in a worker thread
    By inger in forum Newbie
    Replies: 6
    Last Post: 2nd November 2012, 13:37
  2. QAxObject worker thread
    By semajnosnibor in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2012, 17:10
  3. Worker thread
    By doggrant in forum Newbie
    Replies: 4
    Last Post: 3rd November 2009, 16:49
  4. Worker thread problem
    By hkvm in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2009, 21:12
  5. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 23:09

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.