Make ui stay responsive when executin thread
I subclassed QThread and it's run method like this:
mythread.h
Code:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QString>
{
Q_OBJECT
public:
myThread();
void run();
private:
//...
};
#endif // MYTHREAD_H
mythread.cpp
Code:
#include "mythread.h"
#include <iostream>
myThread::myThread()
{
//...
}
void myThread::run()
{
for(int i=0; i<10; i++)
{
std::cout << i << std::endl;
}
}
How and when can i call this thread in order que keep the application responsive?
Sorry if it makes no sense ... my first thread experience :)
Re: Make ui stay responsive when executin thread
Quote:
How and when can i call this thread in order que keep the application responsive?
It doesn't matter, whenever you want...:)
Re: Make ui stay responsive when executin thread
How is that?
i'm calling it from QMainWindow and i have to wait 10 seconds before i hava access to the menu again!
Re: Make ui stay responsive when executin thread
Quote:
Originally Posted by
aguleo
i'm calling it
"It", being what exactly?
1 Attachment(s)
Re: Make ui stay responsive when executin thread
I attach "it" :)
I guess i can not call it from that member ... isn't it?
Re: Make ui stay responsive when executin thread
Quote:
How is that?
i'm calling it from QMainWindow and i have to wait 10 seconds before i hava access to the menu again!
No it won't wait, (That is whats Thread's do)
To run the thread you should be calling QThread::start() (not QThread::run()), this will run the thread in it own (new thread) context
If you call QThread::run(), it will execute in the calling thread context.
1 Attachment(s)
Re: Make ui stay responsive when executin thread
Ok ... made some progress.
I was creating the thread inside a member function ... this way it ended unexpectedly ...
But now i have a question. What happens when i call the same option again while it is still running?
I ask because apparently nothing does!
Re: Make ui stay responsive when executin thread
Quote:
But now i have a question. What happens when i call the same option again while it is still running?
If the thread is running, then nothing will happen.
If the thread is not running, the thread will start running, provided all waiting conditions are cleared.
Re: Make ui stay responsive when executin thread
if you want to run another thread instance, then create an array of threads and run them as you require. don't forget to capture their ending event so you know which are running.
if you need to synchronize concurrent access to shared item, conside QSharedMemory or QSempahore.
Re: Make ui stay responsive when executin thread
Maybe you should first tell us what you want the thread to do?
Re: Make ui stay responsive when executin thread
There was nothing specific on my mind. I read something about saving large files to disk in the "C++ GUI programming with Qt4" and got curiouse.
Quote:
Maybe you should first tell us what you want the thread to do?
You mean that the approach will depend on what i want to do ... is it so?
Re: Make ui stay responsive when executin thread
Yes, for many usecases using a thread is either not possible or simply bad idea.
Here, read this: [wiki]Keeping the GUI Responsive[/wiki]