File tree 2 files changed +30
-1
lines changed
2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -34,11 +34,12 @@ QgsTask::QgsTask( const QString &name, Flags flags )
34
34
QgsTask::~QgsTask ()
35
35
{
36
36
Q_ASSERT_X ( mStatus != Running, " delete" , QStringLiteral ( " status was %1" ).arg ( mStatus ).toLatin1 () );
37
-
37
+ mNotFinishedMutex . tryLock (); // we're not guaranteed to already have the lock in place here
38
38
Q_FOREACH ( const SubTask &subTask, mSubTasks )
39
39
{
40
40
delete subTask.task ;
41
41
}
42
+ mNotFinishedMutex .unlock ();
42
43
}
43
44
44
45
qint64 QgsTask::elapsedTime () const
@@ -261,6 +262,7 @@ void QgsTask::processSubTasksForCompletion()
261
262
setProgress ( 100.0 );
262
263
emit statusChanged ( Complete );
263
264
emit taskCompleted ();
265
+ mNotFinishedMutex .tryLock (); // we're not guaranteed to already have the lock in place here
264
266
mNotFinishedMutex .unlock ();
265
267
}
266
268
else if ( mStatus == Complete )
@@ -288,6 +290,7 @@ void QgsTask::processSubTasksForTermination()
288
290
289
291
emit statusChanged ( Terminated );
290
292
emit taskTerminated ();
293
+ mNotFinishedMutex .tryLock (); // we're not guaranteed to already have the lock in place here
291
294
mNotFinishedMutex .unlock ();
292
295
}
293
296
else if ( mStatus == Terminated && !subTasksTerminated )
Original file line number Diff line number Diff line change @@ -269,6 +269,7 @@ class TestQgsTaskManager : public QObject
269
269
void managerWithSubTasks ();
270
270
void managerWithSubTasks2 ();
271
271
void managerWithSubTasks3 ();
272
+ void cancelBeforeStart ();
272
273
};
273
274
274
275
void TestQgsTaskManager::initTestCase ()
@@ -1348,5 +1349,30 @@ void TestQgsTaskManager::managerWithSubTasks3()
1348
1349
QCOMPARE ( manager3.dependencies ( subTask2Id ), QSet< long >() );
1349
1350
}
1350
1351
1352
+ void TestQgsTaskManager::cancelBeforeStart ()
1353
+ {
1354
+ // add a lot of tasks to the manager, so that some are queued and can't start immediately
1355
+ // then cancel them all!
1356
+ QList< QgsTask * > tasks;
1357
+ QgsTaskManager manager;
1358
+ for ( int i = 0 ; i < 30 ; ++i )
1359
+ {
1360
+ QgsTask *task = new CancelableTask ();
1361
+ tasks << task;
1362
+ manager.addTask ( task );
1363
+ }
1364
+
1365
+ for ( QgsTask *t : qgis::as_const ( tasks ) )
1366
+ {
1367
+ t->cancel ();
1368
+ }
1369
+
1370
+ while ( manager.countActiveTasks () > 1 )
1371
+ {
1372
+ QCoreApplication::processEvents ();
1373
+ }
1374
+ flushEvents ();
1375
+ }
1376
+
1351
1377
QGSTEST_MAIN ( TestQgsTaskManager )
1352
1378
#include " testqgstaskmanager.moc"
You can’t perform that action at this time.
0 commit comments