PDA

View Full Version : QThread : run() Vs start()



cs_raja
18th November 2006, 03:26
What is the difference between run() and start() methods of QThread

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


Thankx

munna
18th November 2006, 05:21
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.

manivannan_1984
21st November 2006, 11:24
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

jpn
21st November 2006, 11:36
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.

^NyAw^
21st November 2006, 11:36
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).