暂停/恢复Qthread从Worker [英] Pause / resume Qthread from Worker
问题描述
在Worker类中,我有两个函数来工作和控制线程,start()和abort()
In the Worker Class I have two functions who works and control the thread, start() and abort()
void Worker::requestWork()
{
mutex.lock();
_working = true;
_abort = false;
qDebug()<<"Le thread travail de"<<this->myId<<" "<<thread()->currentThreadId();
mutex.unlock();
emit workRequested();
}
void Worker::abort()
{
mutex.lock();
if(_working) {
_abort = true;
qDebug()<<"Le thread "<<thread()->currentThreadId()<<" s'arrete";
}
mutex.unlock();
}
正如你可以看到workrequest发出一个信号,工作。并且因为Worker类是在线程中,我如何暂停或恢复呢?从工人类?从MainWindow?
As you can see the workrequest emit a signal saying to the thread to start to work. And because the Worker Class is in the thread, how can I pause or restore it? From the worker class? from the MainWindow?
,现在是Whole code.Mainwindow类。
and now the Whole code.Mainwindow Class.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(command(QString)));
thread = new QThread();
worker = new Worker();
worker->moveToThread(thread);
connect(worker, SIGNAL(valueChanged(QString)), ui->label, SLOT(setText(QString)));
connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
}
//QLineEdit send signal to slot "command" permit to control the thread
void MainWindow::command(QString text){
qDebug()<<"le message a bien ete intercepte";
qDebug()<<ui->lineEdit->text();
if (text.contains("help"))
qDebug()<<"heeeelp";
if (text.contains("pause")){
worker->Paused();
if(thread->isRunning()){}
//cond.wait()
}
if (text.contains("restart")){
worker->Restarted();
if (!thread->isRunning()){}
//cond.wakeAll();
}
if (text.contains("stopped")){
worker->Paused();
thread->wait();
}
if (text.contains("start")){
worker->requestWork();
}
if (text.contains("destroyed")){
worker->destroyed();
}
}
所以我的问题是:当用户插入Pause时暂停并重新启动线程?
So my question is: how to pause and restart the thread from the command() slot when the User insert "Pause" ?
推荐答案
使用QThread工作的两种可能的方式:
I think you mess up the two possible ways of doing work with QThread:
-
一个是将一个对象(Worker)移动到
start()
ed QThread。这样,你通过调用Worker上的槽来工作,当槽执行时完成工作。 您不要暂停/恢复此 - 当插槽完成时,QThread将等待新的工作,QApplication等待事件时空闲的方式相同。
One is to have an object (Worker) moved to a
start()
ed QThread. This way you do work by calling slots on the Worker, the work is done when the slots execute. You don't pause/resume this - when the slot is done, QThread will wait for new work, the same way QApplication waits for events when it's idle.
第二种方法是子类化QThread,重新实现run()和创建(某种类型)Worker。使用这种方式,你必须创建一个类似于状态变量(_working,_done,等待条件等)的工作队列,因为线程将退出,如果它离开run(),你必须暂停/恢复自己。
Second way is subclassing QThread, reimplementing run() and creating (some sort of) Worker there. Using this way you must create your "work queue" with something similar to your state variables (_working, _done, wait conditions, etc), because the thread will exit if it leaves run(), you must pause/resume yourself.
这篇关于暂停/恢复Qthread从Worker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!