Skip to content

Commit

Permalink
Add QgsTask::waitForFinished
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 11, 2017
1 parent 05a713f commit 1f04188
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/qgstaskmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgstaskmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

/**
Expand Down

0 comments on commit 1f04188

Please sign in to comment.