QThread start() return value

QThread start() return value

本文关键字:return value start QThread      更新时间:2023-10-16

我有一个继承QThread的类。QThread的start((函数不返回任何值。当我调用类的 start(( 函数时,我怎么知道线程是否成功启动?

嗨,我不知道你的代码是什么,但这里有一个如何管理线程的示例(其中一个导致有很多线程方法,完全取决于你,你想要什么以及你使用的方法。在我给你的示例中,你看了一下 ScanThread.cpp在那里你看到了 qDebug(( 对我来说,我知道发生了什么事情,比如 (qDebug(( <<"试图打开黑名单";)在这里我知道线程正在尝试打开黑名单,如果线程没有运行 qDebug(( 不会在应用程序输出上显示消息。要知道线程是否正在运行,您还有 isRunning(( 方法,可以在我给你的程序中为我使用有点像 start(( 作为示例,我记录一个文件来告诉我的程序线程已结束并且可以关闭,否则无法关闭它(所以线程读取日志文件,如果他读取 #1313,则主窗口读取它,这意味着他可以关闭。对您来说,如果日志文件 = " 然后关闭。要关闭,您可以使用QCloseEvent,就像我在这里所做的那样。

ScanThread.h

#ifndef SCANTHREAD_H
#define SCANTHREAD_H
#include <QtCore>
class ScanThread :public QThread
{
public:
ScanThread();
void run() ;
};
#endif // SCANTHREAD_H

扫描线程.cpp

#include "scanthread.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QDebug>
QString Root ;
QString Path ;
QString Match ;
QString BlackList = ":/BlackList.txt" ;
ScanThread::ScanThread()
{
}
void ScanThread::run()
{
/* Getting storage name C:/ D:/ E:/ etc..*/
QStorageInfo storage_name = QStorageInfo::root() ;
Root = storage_name.rootPath() ;
QDirIterator Load_Path(Root, QDirIterator::Subdirectories) ;
/* scannning all possible path of the principal dir */
do
{
QFile Write_Path("path.temp") ;
if(Write_Path.open(QFile::WriteOnly | QFile::Text | QFile::Append))
{
QTextStream out(&Write_Path) ;
Path = QString (Load_Path.next()) ;
out << Path << endl ;
qDebug() << "im looking for environment " << Path ;
}
Write_Path.flush() ;
Write_Path.close() ;
}while(Load_Path.hasNext()) ;
/* setuping the scan */
qDebug() << "Trying to setup the scan " ;
QFile Read_Path("path.temp") ;
QFile Read_Blacklist(BlackList) ;
if(Read_Path.open(QFile::ReadOnly | QFile::Text))
{
QTextStream in(&Read_Path) ;
/* read path.temp(contain all dir path */
qDebug() << "Trying to read path.temp" ;
while(!Read_Path.atEnd())
{
Scan:
Path = in.readLine() ;
if(Path == "")
{
goto EndScan ;
}
else
{
/* look for path one by one */
QFile Logs_CurrentPath("CurrentP.temp") ;
if(Logs_CurrentPath.open(QFile::WriteOnly | QFile::Text))
{
QTextStream out(&Logs_CurrentPath) ;
out << Path ;
}
Logs_CurrentPath.flush() ;
Logs_CurrentPath.close() ;
}
/* open blacklist from ressource */
qDebug() << "Trying to open the blacklist " ;

if(Read_Blacklist.open(QFile::ReadOnly | QFile::Text))
{
/* compare path file and words in blacklist */
QTextStream in(&Read_Blacklist) ;
for(int i = 0 ; i < 62218 ; i ++)
{
Match = in.readLine() ;
QFile Logs_ListState("ListState.temp") ;
if(Logs_ListState.open(QFile::WriteOnly | QFile::Text))
{
QTextStream out(&Logs_ListState) ;
out << i ;
}
Logs_ListState.flush() ;
Logs_ListState.close() ;
qDebug() << "Scanning for potential threat" ;
qDebug() << Match ;
qDebug() << Path ;
if(Path.contains(Match) == true)
{
/* theres a threat do something */
}
}
}
Read_Blacklist.flush() ;
Read_Blacklist.close() ;
goto Scan ;
}
/* end the scan once Read_Path is at end */
if(Read_Path.atEnd())
{
EndScan:
qDebug() << "Scan Complete ";

Read_Path.flush() ;
Read_Path.close() ;
QFile WriteEnd("CurrentP.temp") ;
if(WriteEnd.open(QFile::WriteOnly | QFile::Text))
{
QTextStream out(&WriteEnd) ;
out << "#1313" << endl ; //#1313 dev code to manually signal that the scans as been completed
}
WriteEnd.flush() ;
WriteEnd.close() ;
/* terminate thread */
ScanThread::terminate() ;
}
}
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void Queud() ;
protected:
void closeEvent(QCloseEvent *e) ;
private:
Ui::MainWindow *ui;
QTimer *timer ;
};
#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "scanthread.h"
#include <QMessageBox>
#include <QStorageInfo>
#include <QDirIterator>
#include <QFileDialog>
#include <QTimer>
#include <QDebug>
#include <QtCore>
#include <QString>
#include <QCloseEvent>
using namespace std ;
QString Read_CurrentPath ;
float LoadBar = 0 ;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
/* launch timer to update ui */
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(Queud()));
timer->start(1000);
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Queud()
{
/* looking for list state wich word is beign tested */
QFile ReadLog_Count("ListState.temp") ;
if(ReadLog_Count.open(QFile::ReadOnly | QFile::Text))
{
QTextStream in(&ReadLog_Count) ;
in >> LoadBar ;
//qDebug() << LoadBar ;
}
ReadLog_Count.flush() ;
ReadLog_Count.close() ;
/* looking wich current path is beign tested */
QFile ReadLog_CurrentPath("CurrentP.temp") ;
if(ReadLog_CurrentPath.open(QFile::ReadOnly | QFile::Text))
{
QTextStream in(&ReadLog_CurrentPath) ;
Read_CurrentPath = in .readLine();
//qDebug() << read_temp ;
}
ReadLog_CurrentPath.flush() ;
ReadLog_CurrentPath.close() ;
LoadBar = (LoadBar / 62218) * 100 ;
if(Read_CurrentPath == "")
{
/* theres an error the path caint be empty */
}
else
{
/* receive the signal #1313 mean boss i finish my work */ 
if(Read_CurrentPath == "#1313") //#1313 dev code to manually signal that the scans as been completed
{
LoadBar = 0 ;
ui->Path_Status->setText("Scan Complete") ;
ui->LoadBar->setValue(0) ;
timer->stop() ;
}
else
{
ui->Path_Status->setText(Read_CurrentPath) ;
}
}
/* update load bar as it needed */
if(LoadBar == 0)
{
}
else
{
ui->LoadBar->setValue(LoadBar);
}
}
/* close program handling */
void MainWindow::closeEvent(QCloseEvent *e)
{
/* can close normally */
if(Read_CurrentPath == "#1313")
{
timer->stop() ;
e->accept() ;
}
/* dont close the program untill scan complete its not a --->bloatware<--- */
else
{
QMessageBox MSG ;
MSG.setText("You are about to close ,the scan is incomplete.n Leaving while the scan still running can occur errors n please wait..");
MSG.exec() ;

e->ignore() ;
}
}

主.cpp

#include "mainwindow.h"
#include "scanthread.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
/* start that thread */ 
ScanThread sThread ;
sThread.start() ;
return a.exec();
}

你从 QThread 继承了你的类,所以你可以轻松地调用函数start();

您也可以调用函数isRunning();quit();

检查此示例:

void SecondClass::startThread()
{
if(!isRunning()) 
{
start();
// use signal to find out thread is running or not
connect(this, &QThread::started, this, &SecondClass::yourSlotName);
//if thread is running qDebug
if(isRunning())
qDebug()<<"Thread Started."
}
}