Navigation Menu

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
* - calling initTimer( this ) in constructor
* - 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>
class QgsConnectionPoolGroup
Expand Down Expand Up @@ -88,10 +91,8 @@ class QgsConnectionPoolGroup
// no need to run if nothing can expire
if ( conns.isEmpty() )
{
if ( QThread::currentThread() == qApp->thread() )
expirationTimer->stop();
else
QgsDebugMsg( "expirationTimer not stopped from thread" ); // TODO use signals in that case?
// will call the slot directly or queue the call (if the object lives in a different thread)
QMetaObject::invokeMethod( expirationTimer->parent(), "stopExpirationTimer" );
}

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

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

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:
void handleConnectionExpired() { onConnectionExpired(); }
void startExpirationTimer() { expirationTimer->start(); }
void stopExpirationTimer() { expirationTimer->stop(); }

protected:
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:
void handleConnectionExpired() { onConnectionExpired(); }
void startExpirationTimer() { expirationTimer->start(); }
void stopExpirationTimer() { expirationTimer->stop(); }

protected:
Q_DISABLE_COPY( QgsSpatiaLiteConnPoolGroup )
Expand Down

0 comments on commit 2081fc9

Please sign in to comment.