@@ -128,10 +128,13 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
128
128
request.setAttribute ( QNetworkRequest::CacheSaveControlAttribute, true );
129
129
}
130
130
131
- QWaitCondition waitCondition;
131
+ QWaitCondition mainThreadWaitCondition;
132
+ QMutex mainThreadMutex;
132
133
133
- QMutex mutex;
134
- std::function<bool ()> downloaderFunction = [ this , request, synchronous, &waitCondition ]()
134
+ QWaitCondition downloaderThreadWaitCondition;
135
+ QMutex downloaderThreadMutex;
136
+
137
+ std::function<bool ()> downloaderFunction = [ this , request, synchronous, &mainThreadMutex, &mainThreadWaitCondition, &downloaderThreadMutex, &downloaderThreadWaitCondition ]()
135
138
{
136
139
if ( QThread::currentThread () != QgsApplication::instance ()->thread () )
137
140
QgsNetworkAccessManager::instance ( Qt::DirectConnection );
@@ -145,26 +148,36 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
145
148
mErrorCode = QgsWfsRequest::NetworkError;
146
149
mErrorMessage = errorMessageFailedAuth ();
147
150
QgsMessageLog::logMessage ( mErrorMessage , tr ( " WFS" ) );
148
- waitCondition .wakeAll ();
151
+ mainThreadWaitCondition .wakeAll ();
149
152
success = false ;
150
153
}
151
154
else
152
155
{
153
156
// We are able to use direct connection here, because we
154
- // * either run on the thread mReply lives in, so DirectConnection is standard and safe anyway (if it is not the main thread)
157
+ // * either run on the thread mReply lives in, so DirectConnection is standard and safe anyway
155
158
// * or the owner thread of mReply is currently not doing anything because it's blocked in future.waitForFinished() (if it is the main thread)
156
159
connect ( mReply , &QNetworkReply::finished, this , &QgsWfsRequest::replyFinished, Qt::DirectConnection );
157
160
connect ( mReply , &QNetworkReply::downloadProgress, this , &QgsWfsRequest::replyProgress, Qt::DirectConnection );
158
- connect ( QgsNetworkAccessManager::instance (), &QgsNetworkAccessManager::authenticationRequired, this , [&waitCondition]() { waitCondition.wakeAll (); } );
159
161
160
162
if ( synchronous )
161
163
{
164
+ connect ( QgsNetworkAccessManager::instance (), &QgsNetworkAccessManager::authenticationRequired, this , [&mainThreadMutex, &mainThreadWaitCondition, &downloaderThreadMutex, &downloaderThreadWaitCondition]()
165
+ {
166
+ mainThreadMutex.lock ();
167
+ mainThreadWaitCondition.wakeAll ();
168
+ mainThreadMutex.unlock ();
169
+
170
+ downloaderThreadMutex.lock ();
171
+ downloaderThreadWaitCondition.wait ( &downloaderThreadMutex );
172
+ downloaderThreadMutex.unlock ();
173
+ }, Qt::DirectConnection
174
+ );
162
175
QEventLoop loop;
163
176
connect ( this , &QgsWfsRequest::downloadFinished, &loop, &QEventLoop::quit, Qt::DirectConnection );
164
177
loop.exec ();
165
178
}
166
179
}
167
- waitCondition .wakeAll ();
180
+ mainThreadWaitCondition .wakeAll ();
168
181
return success;
169
182
};
170
183
@@ -176,9 +189,16 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
176
189
downloaderThread->start ();
177
190
while ( !downloaderThread->isFinished () )
178
191
{
179
- waitCondition.wait ( &mutex );
192
+ mainThreadMutex.lock ();
193
+ mainThreadWaitCondition.wait ( &mainThreadMutex );
194
+ mainThreadMutex.unlock ();
180
195
if ( !downloaderThread->isFinished () )
196
+ {
181
197
QgsApplication::instance ()->processEvents ();
198
+ downloaderThreadMutex.lock ();
199
+ downloaderThreadWaitCondition.wakeAll ();
200
+ downloaderThreadMutex.unlock ();
201
+ }
182
202
}
183
203
184
204
success = downloaderThread->success ();
@@ -187,7 +207,6 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
187
207
{
188
208
success = downloaderFunction ();
189
209
}
190
- mutex.unlock ();
191
210
return success && mErrorMessage .isEmpty ();
192
211
}
193
212
0 commit comments