Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was focusing on the QtConcurrent module, since you mentioned it in your question. But you can achieve your goal also with plain QThread:</p> <pre><code>// A Thread calling your test function class MyThread : public QThread { protected: void run() { myFunction(); } }; // calling the function MyThread t; t.start(); if ( t.wait(30000) ) { // Finished } else { // Not finished // ATTENTION: USE TERMINATE WITH CARE (see QThread documentation)! t.terminate(); } </code></pre> <p>The call to terminate will forceably stop the thread and thus stop the execution of your function. Note however that the thread cannot cleanup, and any resources used by your function are not freed properly.</p> <hr> <p><strong>Old Answer:</strong><br> You can do this with Qt, using a <code>QFutureWatcher</code>, a <code>QTimer</code>, and a helper <code>QEventLoop</code> . Setup your concurrent run, and watch the result with the watcher. Set the Timout with the timer, and wait until either is finished, in a Event loop.</p> <pre><code>// Setup eventloop, watcher and timer QEventLoop loop; QFutureWatcher watcher; QObject::connect( &amp;watcher, SIGNAL(finished()), &amp;loop, SLOT(quit())); QTimer::singleShot( 30000, &amp;loop, SLOT(quit()) ); // Start function call QFuture&lt;T&gt; future = QtConcurrent::run(...); // Watch the call watcher.setFuture( future ); // Wait until event loop finishes loop.exec(); // Now either future has finished, or timeout was reached... if ( future.isFinished() ) { // Function completed! } else { future.cancel(); // Infinite loop... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload