Skip to content

Commit

Permalink
Fix the issues with QTimer start/stop in different thread
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Feb 28, 2014
1 parent 9b893a3 commit 2081fc9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/core/qgsconnectionpool.h
Expand Up @@ -43,6 +43,9 @@
* - being derived from QObject * - being derived from QObject
* - calling initTimer( this ) in constructor * - calling initTimer( this ) in constructor
* - having handleConnectionExpired() slot that calls onConnectionExpired() * - having handleConnectionExpired() slot that calls onConnectionExpired()
* - having startExpirationTimer(), stopExpirationTimer() slots to start/stop the expiration timer
*
* For an example on how to use the template class, have a look at the implementation in postgres/spatialite providers.
*/ */
template <typename T> template <typename T>
class QgsConnectionPoolGroup class QgsConnectionPoolGroup
Expand Down Expand Up @@ -88,10 +91,8 @@ class QgsConnectionPoolGroup
// no need to run if nothing can expire // no need to run if nothing can expire
if ( conns.isEmpty() ) if ( conns.isEmpty() )
{ {
if ( QThread::currentThread() == qApp->thread() ) // will call the slot directly or queue the call (if the object lives in a different thread)
expirationTimer->stop(); QMetaObject::invokeMethod( expirationTimer->parent(), "stopExpirationTimer" );
else
QgsDebugMsg( "expirationTimer not stopped from thread" ); // TODO use signals in that case?
} }


return i.c; return i.c;
Expand Down Expand Up @@ -120,10 +121,8 @@ class QgsConnectionPoolGroup


if ( !expirationTimer->isActive() ) if ( !expirationTimer->isActive() )
{ {
if ( QThread::currentThread() == qApp->thread() ) // will call the slot directly or queue the call (if the object lives in a different thread)
expirationTimer->start(); QMetaObject::invokeMethod( expirationTimer->parent(), "startExpirationTimer" );
else
QgsDebugMsg( "expirationTimer not started from thread" ); // TODO use signals in that case?
} }


connMutex.unlock(); connMutex.unlock();
Expand Down
2 changes: 2 additions & 0 deletions src/providers/postgres/qgspostgresconnpool.h
Expand Up @@ -46,6 +46,8 @@ class QgsPostgresConnPoolGroup : public QObject, public QgsConnectionPoolGroup<Q


protected slots: protected slots:
void handleConnectionExpired() { onConnectionExpired(); } void handleConnectionExpired() { onConnectionExpired(); }
void startExpirationTimer() { expirationTimer->start(); }
void stopExpirationTimer() { expirationTimer->stop(); }


protected: protected:
Q_DISABLE_COPY( QgsPostgresConnPoolGroup ) Q_DISABLE_COPY( QgsPostgresConnPoolGroup )
Expand Down
2 changes: 2 additions & 0 deletions src/providers/spatialite/qgsspatialiteconnpool.h
Expand Up @@ -45,6 +45,8 @@ class QgsSpatiaLiteConnPoolGroup : public QObject, public QgsConnectionPoolGroup


protected slots: protected slots:
void handleConnectionExpired() { onConnectionExpired(); } void handleConnectionExpired() { onConnectionExpired(); }
void startExpirationTimer() { expirationTimer->start(); }
void stopExpirationTimer() { expirationTimer->stop(); }


protected: protected:
Q_DISABLE_COPY( QgsSpatiaLiteConnPoolGroup ) Q_DISABLE_COPY( QgsSpatiaLiteConnPoolGroup )
Expand Down

0 comments on commit 2081fc9

Please sign in to comment.