QThread - 调用另一个线程的方法

时间:2014-03-09 23:27:55

标签: c++ multithreading qt signals slot

我正在尝试使用invokeMethod调用另一个线程(不是Main)的方法。不幸的是,只要它尝试执行invokeMethod就会使应用程序崩溃!

我错了什么?

// main.cpp

#include <QtCore>
#include "entrypointclass.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug() << a.thread()->currentThreadId() << " - Application started.";

    EntryPointClass entryPoint;
    entryPoint.runInNewThread();
    return a.exec();
}

// Entrypoint.h

#ifndef ENTRYPOINTCLASS_H
#define ENTRYPOINTCLASS_H

#include "worker.h"
#include <QtCore>

class EntryPointClass : public QObject
{
    Q_OBJECT
public:
    EntryPointClass();
    ~EntryPointClass();
    void runInNewThread();
public slots:
    void timeoutExpired();
private:
    Worker* m_Worker;
    QThread* m_Thread;
};

#endif // ENTRYPOINTCLASS_H

// Entrypoint.cpp

#include <QTCore>
#include "entrypointclass.h"
#include "Worker.h"

EntryPointClass::EntryPointClass()
{
    qDebug() << "EntryPointClass created";



}

EntryPointClass::~EntryPointClass()
{
    qDebug() << "EntryPointClass destroyed";
}

void EntryPointClass::runInNewThread()
{
    QThread* m_Thread = new QThread;
    Worker* m_Worker = new Worker();


    connect(m_Thread, SIGNAL(started()), m_Worker, SLOT(doSomething()));
    connect(m_Worker, SIGNAL(finished()), m_Thread, SLOT(quit()));
    connect(m_Thread, SIGNAL(finished()), m_Thread, SLOT(deleteLater()));
    connect(m_Thread, SIGNAL(finished()), m_Worker, SLOT(deleteLater()));

    QTimer* timer = new QTimer;
    timer->setSingleShot(true);

    //bool bOK = connect(timer, SIGNAL(timeout()), m_Worker, SLOT(closeWorker()), Qt::BlockingQueuedConnection);

    connect(timer, SIGNAL(timeout()), this, SLOT(timeoutExpired()));

    m_Worker->moveToThread(m_Thread);
    m_Thread->start();
    timer->start(5000);

}

void EntryPointClass::timeoutExpired()
{
    qDebug() << "timeout expired";
    if (m_Worker != NULL)
    {
        QMetaObject::invokeMethod(m_Worker, "closeWorker", Qt::QueuedConnection);
    }
}

// Worker.h

#ifndef WORKER_H
#define WORKER_H

#include <QtCore>

class Worker : public QObject
{
    Q_OBJECT
public:
    Worker();
    ~Worker();

public slots:
    void doSomething();
    void closeWorker();

private:
    bool m_bAbort;
    QMutex m_mutex;


signals:
    void finished();
};

#endif // WORKER_H

// Worker.cpp

#include "worker.h"
#include <unistd.h>
#include "QTcore"

Worker::Worker()
    : m_mutex()
{
    qDebug() << this->thread()->currentThreadId() << "Worker created";
    m_bAbort = false;
    //qDebug() << QString("Thread %1 - Worker created").arg("");//this->thread()->currentThreadId());
}

Worker::~Worker()
{
    qDebug() << this->thread()->currentThreadId() << "Worker destroyed";

}

void Worker::doSomething()
{
    while(!m_bAbort)
    {
        sleep(2);
        qDebug() << this->thread()->currentThreadId() << "Do Something!";
    }

    emit finished();
}

void Worker::closeWorker()
{
    qDebug() << this->thread()->currentThreadId() << "closeWorker triggered!";
    QMutexLocker mutexLocker(&m_mutex);
    m_bAbort = true;
}

1 个答案:

答案 0 :(得分:0)

不要直接调用方法,而是尝试发布自定义事件。这将要求您在处理自定义事件的目标上实现事件过滤器。只要事件循环在目标线程中处于活动状态,事件发布就会一直有效。

看看这个答案herehere也是{{3}}。 希望这会有所帮助。