Add QgsTask::waitForFinished

This commit is contained in:
Matthias Kuhn 2017-05-10 11:36:52 +02:00
parent 05a713f444
commit 1f041884bb
2 changed files with 29 additions and 0 deletions

View File

@ -142,6 +142,27 @@ QList<QgsMapLayer *> QgsTask::dependentLayers() const
return _qgis_listQPointerToRaw( mDependentLayers );
}
bool QgsTask::waitForFinished( int timeout )
{
QEventLoop loop;
bool rv = true;
connect( this, &QgsTask::taskCompleted, &loop, &QEventLoop::quit );
connect( this, &QgsTask::taskTerminated, &loop, &QEventLoop::quit );
QTimer timer;
if ( timeout != -1 )
{
timer.start( timeout );
connect( &timer, &QTimer::timeout, [&rv]() { rv = false; } );
connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
}
loop.exec();
return rv;
}
void QgsTask::setDependentLayers( const QList< QgsMapLayer * > &dependentLayers )
{
mDependentLayers = _qgis_listRawToQPointer( dependentLayers );

View File

@ -186,6 +186,14 @@ class CORE_EXPORT QgsTask : public QObject
*/
QList< QgsMapLayer * > dependentLayers() const;
/**
* Blocks the current thread until the task finishes or a maximum of \a timeout milliseconds.
* If the \a timeout is ``-1`` the thread will be blocked forever.
*
* The result will be false if the wait timed out and true in any other case.
*/
bool waitForFinished( int timeout = 30000 );
signals:
/**