Results 1 to 5 of 5

Thread: QThread : run() Vs start()

  1. #1
    Join Date
    Jun 2006
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Unhappy QThread : run() Vs start()

    What is the difference between run() and start() methods of QThread

    WIll they make any difference on the GUI??? like freezing ???


    Thankx

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread : run() Vs start()

    From the docs


    QThread begins executing in run(). To create your own threads, subclass QThread and reimplement run()

    Use the start() method to begin execution. Execution ends when you return from run(), just as an application does when it leaves main()
    To create your own thread you'll always need to subclass QThread and reimplement run().
    To start the thread you'll need to call start().

    I hope it's clear now.

  3. #3
    Join Date
    Aug 2006
    Location
    banglore
    Posts
    21
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QThread : run() Vs start()

    Quote Originally Posted by munna View Post
    From the docs



    To create your own thread you'll always need to subclass QThread and reimplement run().
    To start the thread you'll need to call start().

    We can also call the thread by using run() directly know, then why we need start(), pls clarify me...

    Regards,
    Mani

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread : run() Vs start()

    No, you won't get any new thread started if you call run().

    QThread is started by calling QThread::start().

    QThread::run() must be reimplemented to define what is being done in the thread when it has been started.
    J-P Nurmi

  5. #5
    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: QThread : run() Vs start()

    Hi,

    You haven't to call anyway run() method of Thread class. You have to reimplement it but when you what to start the Thread you have to call start() method.

    start() method has a lot of code that really creates the Thread and finally calls the run() method. When you call start() you let the SO create a Thread that has its own memory space and its own code space, then, the run() method will be invoked.

    So, if you want your Thread really starts concurrently from the Main Thread you have to call start(). If you call run() the main Thread will execute directly the run() method, so if there is a infinite loop (sure that you created a infinite loop controlled by a variable), the main Thread will stay in the loop and will hang the response to the GUI because the Main Thread is who controlls the events in the GUI. This is the reason because the Main Thread is also called GUI Thread.

    Other problem is that a Thread cannot use GUI objects to make them for example paint an image (I had a trouble with this also).
    Òscar Llarch i Galán

Similar Threads

  1. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  2. Replies: 4
    Last Post: 10th May 2006, 22:37
  3. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51
  4. Replies: 6
    Last Post: 17th March 2006, 17:48
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

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.