0

I have a question, I am trying to find the most simple way (without eventloop and or QFutureWatcher) to run a function in a different thread without blocking the gui. I have an exporter object from other library, can not change it, and its Close() function takes about 15 seconds to close.

So I tried this trick below but it works only for a couple of seconds, but then the processEvents() function blocks until the Export function is finished which makes it useless.

My Code:

QFuture<void> future = QtConcurrent::run([&]() {exporter.Close(); 
                        qDebug() << "closed!";});

                    while (!future.isFinished())
                    {       
                        QApplication::processEvents();
                        QThread::msleep(1); //yield
                        qDebug() << "z";
                    }
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Richy
  • 534
  • 4
  • 13
  • What type is `exporter`? Is it `Qt` based? Inherits -- directly or otherwise -- from `QObject`? – G.M. Mar 10 '20 at 10:01
  • Can you clarify what you mean by `"the processEvents() function blocks until the Export function is finished"`? Do you mean the call to `processEvents` in the `while` loop shown or does the blocking occur *after* the `while` loop has finished and your code has returned to the main exec event loop? – G.M. Mar 10 '20 at 18:15
  • it's not clear what is the problem; the title refers to 'early end of isFinished' but the content refers to 'blocking processEvents' – Ľubomír Carik Mar 15 '20 at 02:54
  • You have to check how long time does `exporter.Close()` actually takes in the debugger. Exporter class may be not thread-safe and you need to check this, and also check whether`exporter.Close()` return any erros. – Vladimir Bershov Mar 26 '20 at 17:20

0 Answers0