Merge pull request #71 from mammonsama666/cpu

fix(read queue thread): Fix bug of excessive CPU usage.
This commit is contained in:
iaom 2021-01-13 09:34:38 +08:00 committed by GitHub
commit 1df1a05e58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 4 deletions

View File

@ -162,7 +162,7 @@ void MainWindow::initUi()
connect(m_settingsWidget, &SettingsWidget::settingWidgetClosed, this, [ = ]() { connect(m_settingsWidget, &SettingsWidget::settingWidgetClosed, this, [ = ]() {
QTimer::singleShot(100, this, [ = ] { QTimer::singleShot(100, this, [ = ] {
clearSearchResult(); clearSearchResult();
m_search_result_thread->start(); // m_search_result_thread->start();
this->setWindowState(this->windowState() & ~Qt::WindowMinimized); this->setWindowState(this->windowState() & ~Qt::WindowMinimized);
this->raise(); this->raise();
this->showNormal(); this->showNormal();
@ -192,10 +192,17 @@ void MainWindow::initUi()
&MainWindow::primaryScreenChangedSlot); &MainWindow::primaryScreenChangedSlot);
connect(m_searchLayout, &UkuiSearchBarHLayout::textChanged, this, [ = ](QString text) { connect(m_searchLayout, &UkuiSearchBarHLayout::textChanged, this, [ = ](QString text) {
if (text == "") { if (text == "") {
if (m_search_result_thread->isInterruptionRequested()) {
m_search_result_thread->requestInterruption();
m_search_result_thread->quit();
}
m_contentFrame->setCurrentIndex(0); m_contentFrame->setCurrentIndex(0);
} else { } else {
m_contentFrame->setCurrentIndex(1); m_contentFrame->setCurrentIndex(1);
// QTimer::singleShot(50,this,[=](){ // QTimer::singleShot(50,this,[=](){
if (! m_search_result_thread->isInterruptionRequested()) {
m_search_result_thread->start();
}
searchContent(text); searchContent(text);
// }); // });
} }
@ -215,7 +222,7 @@ void MainWindow::bootOptionsFilter(QString opt)
this->show(); this->show();
this->raise(); this->raise();
this->activateWindow(); this->activateWindow();
m_search_result_thread->start(); // m_search_result_thread->start();
} }
/** /**
@ -299,6 +306,7 @@ bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
switch (event->response_type & ~0x80) { switch (event->response_type & ~0x80) {
case XCB_FOCUS_OUT: case XCB_FOCUS_OUT:
this->hide(); this->hide();
m_search_result_thread->requestInterruption();
m_search_result_thread->quit(); m_search_result_thread->quit();
break; break;
} }

View File

@ -1,20 +1,30 @@
#include "search-result.h" #include "search-result.h"
#include <QTimer>
SearchResult::SearchResult(QObject * parent) : QThread(parent) SearchResult::SearchResult(QObject * parent) : QThread(parent)
{ {
m_mainwindow = static_cast<MainWindow *>(parent); m_mainwindow = static_cast<MainWindow *>(parent);
// m_timer = new QTimer;
// QObject::connect(m_timer, &QTimer::timeout, this, [ = ](){
// qWarning()<<"-------------------------------------------------------";
// m_timer->stop();
// this->requestInterruption();
// });
} }
SearchResult::~SearchResult() SearchResult::~SearchResult()
{ {
// if (m_timer) {
// delete m_timer;
// m_timer = NULL;
// }
} }
void SearchResult::run() void SearchResult::run()
{ {
QTimer * m_timer = new QTimer;
int emptyLists = 0; int emptyLists = 0;
while(!isInterruptionRequested()) { while(!isInterruptionRequested()) {
qWarning()<<"--------------------";
emptyLists = 0; emptyLists = 0;
m_mainwindow->m_searcher->m_mutex1.lock(); m_mainwindow->m_searcher->m_mutex1.lock();
if (!m_mainwindow->m_search_result_file->isEmpty()) { if (!m_mainwindow->m_search_result_file->isEmpty()) {
@ -42,5 +52,15 @@ void SearchResult::run()
emptyLists ++; emptyLists ++;
m_mainwindow->m_searcher->m_mutex3.unlock(); m_mainwindow->m_searcher->m_mutex3.unlock();
} }
if (m_timer->isActive() && m_timer->remainingTime() < 0.01) {
this->requestInterruption();
}
if (emptyLists == 3 && !m_timer->isActive()) {
m_timer->start();
} else if (emptyLists != 3) {
m_timer->stop();
} else {
msleep(100);
}
} }
} }

View File

@ -2,6 +2,7 @@
#define SEARCHRESULT_H #define SEARCHRESULT_H
#include <QThread> #include <QThread>
#include <QWaitCondition> #include <QWaitCondition>
#include <QTimer>
#include "mainwindow.h" #include "mainwindow.h"
class SearchResult : public QThread class SearchResult : public QThread
@ -15,6 +16,7 @@ protected:
private: private:
MainWindow * m_mainwindow = nullptr; MainWindow * m_mainwindow = nullptr;
// QTimer * m_timer = nullptr;
Q_SIGNALS: Q_SIGNALS:
void searchResultFile(const QString&); void searchResultFile(const QString&);