Skip to content

Commit 2081fc9

Browse files
committedFeb 28, 2014
Fix the issues with QTimer start/stop in different thread
1 parent 9b893a3 commit 2081fc9

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed
 

‎src/core/qgsconnectionpool.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
* - being derived from QObject
4444
* - calling initTimer( this ) in constructor
4545
* - having handleConnectionExpired() slot that calls onConnectionExpired()
46+
* - having startExpirationTimer(), stopExpirationTimer() slots to start/stop the expiration timer
47+
*
48+
* For an example on how to use the template class, have a look at the implementation in postgres/spatialite providers.
4649
*/
4750
template <typename T>
4851
class QgsConnectionPoolGroup
@@ -88,10 +91,8 @@ class QgsConnectionPoolGroup
8891
// no need to run if nothing can expire
8992
if ( conns.isEmpty() )
9093
{
91-
if ( QThread::currentThread() == qApp->thread() )
92-
expirationTimer->stop();
93-
else
94-
QgsDebugMsg( "expirationTimer not stopped from thread" ); // TODO use signals in that case?
94+
// will call the slot directly or queue the call (if the object lives in a different thread)
95+
QMetaObject::invokeMethod( expirationTimer->parent(), "stopExpirationTimer" );
9596
}
9697

9798
return i.c;
@@ -120,10 +121,8 @@ class QgsConnectionPoolGroup
120121

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

129128
connMutex.unlock();

‎src/providers/postgres/qgspostgresconnpool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class QgsPostgresConnPoolGroup : public QObject, public QgsConnectionPoolGroup<Q
4646

4747
protected slots:
4848
void handleConnectionExpired() { onConnectionExpired(); }
49+
void startExpirationTimer() { expirationTimer->start(); }
50+
void stopExpirationTimer() { expirationTimer->stop(); }
4951

5052
protected:
5153
Q_DISABLE_COPY( QgsPostgresConnPoolGroup )

‎src/providers/spatialite/qgsspatialiteconnpool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class QgsSpatiaLiteConnPoolGroup : public QObject, public QgsConnectionPoolGroup
4545

4646
protected slots:
4747
void handleConnectionExpired() { onConnectionExpired(); }
48+
void startExpirationTimer() { expirationTimer->start(); }
49+
void stopExpirationTimer() { expirationTimer->stop(); }
4850

4951
protected:
5052
Q_DISABLE_COPY( QgsSpatiaLiteConnPoolGroup )

0 commit comments

Comments
 (0)
Please sign in to comment.