From 772d8fc81e0afcb0a873738cde649e5e640d8a41 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Mon, 9 May 2022 23:03:01 +0800 Subject: fix: avoid block when refreshing keys --- src/ui/UserInterfaceUtils.cpp | 44 +++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index 9859ff65..724a467e 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -330,28 +330,32 @@ void CommonUtils::SlotImportKeyFromKeyServer( void CommonUtils::slot_update_key_status() { LOG(INFO) << "called"; + auto *thread = QThread::create([this]() { + std::vector threads; + + // flush key cache for all GpgKeyGetter Intances. + for (const auto &channel_id : GpgKeyGetter::GetAllChannelId()) { + // multi threading + auto *refresh_thread = QThread::create([channel_id]() { + LOG(INFO) << "FlushKeyCache thread start" + << "channel:" << channel_id; + GpgKeyGetter::GetInstance(channel_id).FlushKeyCache(); + }); + refresh_thread->start(); + threads.push_back(refresh_thread); + } - std::vector threads; - - // flush key cache for all GpgKeyGetter Intances. - for (const auto &channel_id : GpgKeyGetter::GetAllChannelId()) { - // multi threading - auto *thread = QThread::create([channel_id]() { - LOG(INFO) << "thread start" - << "channel:" << channel_id; - GpgKeyGetter::GetInstance(channel_id).FlushKeyCache(); - }); - thread->start(); - threads.push_back(thread); - } - - for (auto *thread : threads) { - thread->wait(); - thread->deleteLater(); - } + for (auto *thread : threads) { + thread->wait(); + thread->deleteLater(); + } - emit SignalKeyDatabaseRefreshDone(); - LOG(INFO) << "finished"; + emit SignalKeyDatabaseRefreshDone(); + LOG(INFO) << "finished"; + }); + connect(thread, &QThread::finished, thread, &QThread::deleteLater); + LOG(INFO) << "start thread"; + thread->start(); } } // namespace GpgFrontend::UI \ No newline at end of file -- cgit v1.2.3