簡體   English   中英

QThread start()返回值

[英]QThread start() return value

我有一個繼承QThread的類。 QThread的start()函數不返回任何值。 當我調用類的start()函數時,如何知道線程是否成功啟動?

嗨,我不知道您的代碼是什么,但是這里是一個如何管理線程的示例(其中一個導致有很多線程方法完全取決於您,您想要什么以及使用哪種方法。在該示例中,我向您介紹了在其中看到qDebug()的ScanThread.cpp,這就是我的工作方式,我知道事情正在發生,例如(qDebug()<<“試圖打開黑名單”;)在這里,我知道線程正在嘗試打開黑名單,如果線程沒有運行qDebug()不會在應用程序輸出上顯示消息。要知道線程是否正在運行,您還可以使用isRunning()方法,該方法可以在我給定的程序中像start()一樣使用以您為例,我記錄了一個文件來告訴我的程序該線程已結束並且可以關閉該線程,否則無法關閉它(因此,如果該線程讀取了#1313,則該線程讀取了該日志文件,而mainwindow讀取了該文件,這意味着他可以關閉對您來說,如果日志文件=“”然后關閉。要關閉,可以像我在這里一樣使用QCloseEvent。

掃描線程

#ifndef SCANTHREAD_H
#define SCANTHREAD_H
#include <QtCore>

class ScanThread :public QThread
{
public:
    ScanThread();
    void run() ;
};

#endif // SCANTHREAD_H

掃描線程

#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();
}

您可以從QThread繼承您的類,因此可以調用start();函數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."
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM