我有一个继承QThread的类。 QThread的start()函数不返回任何值。当我调用我的类的start()函数时,如何知道线程是否成功启动?
答案 0 :(得分:0)
嗨,我不知道您的代码是什么,但是这里是一个如何管理线程的示例(其中一个导致全部依赖您的线程方法,您想要什么以及您使用哪种方法。在我给出的示例中您在ScanThread.cpp上看到qDebug(),这对我来说就是这样,我知道事情正在发生,例如(qDebug()<<“试图打开黑名单”;)在这里,我知道线程正在尝试打开黑名单,如果没有运行qDebug()的线程不会在应用程序输出上显示消息。要知道某个线程是否正在运行,您还可以使用isRunning()方法,该方法可以在代码中使用start()我以您为例的程序为例,我记录了一个文件来告诉我的程序该线程已结束并且可以关闭该线程,否则无法将其关闭(因此,如果该线程读取了#1313,则该线程读取了日志文件,而mainwindow读取了该文件,这意味着他可以关闭。对您来说,如果日志文件=“”然后关闭。要关闭,可以像我在这里一样使用QCloseEvent。
ScanThread.h
#ifndef SCANTHREAD_H
#define SCANTHREAD_H
#include <QtCore>
class ScanThread :public QThread
{
public:
ScanThread();
void run() ;
};
#endif // SCANTHREAD_H
ScanThread.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() ;
}
}
}
MainWindow.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
MainWindow.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() ;
}
}
Main.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();
}
答案 1 :(得分:-1)
您从documentation继承了自己的课程,因此您可以轻松调用函数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."
}
}