Whenever I run a QEventLoop
while waiting for a thread to finish background work, CPU usage goes up to 100% even if the thread does a lot of waiting in its function and doesn't do intense calculations. It's the event loop that loads the CPU. Is there a way to configure QEventLoop
to run more slowly and spare the CPU?
Here is an example:
WorkerThread thread;
QEventLoop eventLoop;
QTimer threadTimer;
threadTimer.setSingleShot(true);
connect(&threadTimer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
connect(&thread, SIGNAL(finished()), &eventLoop, SLOT(quit()));
threadTimer.start(180000);
thread.start();
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
if(threadTimer.isActive())
{
threadTimer.stop();
}