PDA

View Full Version : QThread stop button is not working



hacquerqop
18th July 2018, 10:04
Stop button is not working.why?

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"


Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
mThread = new MyThread(this);
connect(mThread,SIGNAL(NumberChanged(int)),this, SLOT(onNumberChanged (int)) );
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::onNumberChanged(int Number)
{
ui->label->setText(QString::number (Number));
}

void Dialog::on_pushButton_clicked()
{
//start
mThread->start ();
}

void Dialog::on_pushButton_2_clicked() //This part
{
//stop
mThread->Stop = true;
}


myThread.cpp

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

MyThread::MyThread(QObject *parent) :
QThread(parent)
{

}

void MyThread::run()
{
for(int i = 0; i < 10000; i++)
{
QMutex mutex;
mutex.lock();
if(this->Stop) //break;
mutex.unlock();

emit NumberChanged (i);

this->msleep (100);
}

}


dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include"mythread.h"
#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
MyThread * mThread;
private:
Ui::Dialog *ui;

public slots:
void onNumberChanged(int);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();

};

#endif // DIALOG_H

nix
18th July 2018, 11:20
Hi,

Break is commented out. Is that what you want do?

Sincerely

hacquerqop
18th July 2018, 12:36
Hi,

Break is commented out. Is that what you want do?

Sincerely


when i write break it gives me an error QMutex: destroying locked mutex thats why i comment that

nix
18th July 2018, 13:00
The warning is right, you are exiting the run function after the break statement, destroying the mutex in the process without unlocking it.

I don't know what you try to achieve with the mutex but used like that is useless, if it can help you can take a look at qmutexlocker.

d_stranz
18th July 2018, 21:10
Stop button is not working.why?

1 - You have not actually created a thread. You have created a QThread instance in the GUI thread. This does not create an independently executing thread, and calling start() simply results in your MyThread's run() method being called.
2 - Once run() starts, then you are locked in an infinite loop. Calling sleep() or msleep() puts your entire application to sleep for that period of time since you have only one thread running (the GUI thread).
3 - Any time you have a loop that runs for a long time where you do not allow the Qt event loop to process new events, then your application will freeze up. If you have only a single thread, then you must allow control to return to the event loop. One way is to call QCoreApplication::processEvents(). This is why your button click doesn't work.

I'd suggest you read Maya Posch's excellent blog post on multithreading and QThread (https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/). Or buy her book (https://www.amazon.com/Mastering-Multithreading-concurrent-parallel-applications-ebook/dp/B01NBBTHY3).