@@ -130,13 +130,15 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
130
130
131
131
QWaitCondition waitCondition;
132
132
QMutex waitConditionMutex;
133
+ bool threadFinished = false ;
134
+ bool success = false ;
133
135
134
- std::function<bool ()> downloaderFunction = [ this , request, synchronous, &waitConditionMutex, &waitCondition ]()
136
+ std::function<void ()> downloaderFunction = [ this , request, synchronous, &waitConditionMutex, &waitCondition, &threadFinished, &success ]()
135
137
{
136
138
if ( QThread::currentThread () != QgsApplication::instance ()->thread () )
137
139
QgsNetworkAccessManager::instance ( Qt::DirectConnection );
138
140
139
- bool success = true ;
141
+ success = true ;
140
142
mReply = QgsNetworkAccessManager::instance ()->get ( request );
141
143
142
144
mReply ->setReadBufferSize ( READ_BUFFER_SIZE_HINT );
@@ -180,41 +182,51 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
180
182
loop.exec ();
181
183
}
182
184
}
185
+ waitConditionMutex.lock ();
186
+ threadFinished = true ;
183
187
waitCondition.wakeAll ();
184
- return success ;
188
+ waitConditionMutex. unlock () ;
185
189
};
186
190
187
- bool success;
188
-
189
191
if ( synchronous && QThread::currentThread () == QApplication::instance ()->thread () )
190
192
{
191
193
std::unique_ptr<DownloaderThread> downloaderThread = qgis::make_unique<DownloaderThread>( downloaderFunction );
192
194
downloaderThread->start ();
193
195
194
- while ( !downloaderThread-> isFinished () )
196
+ while ( true )
195
197
{
196
198
waitConditionMutex.lock ();
199
+ if ( threadFinished )
200
+ {
201
+ waitConditionMutex.unlock ();
202
+ break ;
203
+ }
197
204
waitCondition.wait ( &waitConditionMutex );
198
- waitConditionMutex.unlock ();
199
205
200
206
// If the downloader thread wakes us (the main thread) up and is not yet finished
201
207
// he needs the authentication to run.
202
208
// The processEvents() call gives the auth manager the chance to show a dialog and
203
209
// once done with that, we can wake the downloaderThread again and continue the download.
204
- if ( !downloaderThread-> isFinished () )
210
+ if ( !threadFinished )
205
211
{
212
+ waitConditionMutex.unlock ();
213
+
206
214
QgsApplication::instance ()->processEvents ();
207
215
waitConditionMutex.lock ();
208
216
waitCondition.wakeAll ();
209
217
waitConditionMutex.unlock ();
210
218
}
219
+ else
220
+ {
221
+ waitConditionMutex.unlock ();
222
+ }
211
223
}
212
-
213
- success = downloaderThread->success ();
224
+ // wait for thread to gracefully exit
225
+ downloaderThread->wait ();
214
226
}
215
227
else
216
228
{
217
- success = downloaderFunction ();
229
+ downloaderFunction ();
218
230
}
219
231
return success && mErrorMessage .isEmpty ();
220
232
}
0 commit comments