// mainwindow.h ====================================================================
void MainWindow::on_pushButton_Create_clicked()
{
pth = new TestThread(this);
pth->moveToThread(clientthread);
//connect(clientthread, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCT(QObject* )));
//connect(CT, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCST(QObject* )));
connect(clientthread, SIGNAL(started()), pth, SLOT(process()));
//connect(CT, SIGNAL(finished()), clientthread, SLOT(quit()));
//connect(CT, SIGNAL(finished()), CT, SLOT(deleteLater()));
//connect(clientthread, SIGNAL(finished()), clientthread, SLOT(deleteLater()));
clientthread->start();
ui->pushButton_Create->setDisabled(true);
ui->pushButton_Delete->setEnabled(true);
}
void MainWindow::on_pushButton_Delete_clicked()
{
pth->stop();
while(!pth->isStopped);
clientthread->terminate();
//while(!clientthread->isRunning()); // why doesn't work ???
delete pth;
delete clientthread;
ui->pushButton_Create->setEnabled(true);
ui->pushButton_Delete->setDisabled(true);
}
// thread.h ===========================================================================
{
Q_OBJECT
public:
explicit TestThread
(QObject * parent
);
volatile bool isStopped;
private:
volatile bool isStopping;
public slots:
void process();
void stop();
};
// thread.cpp =====================================================================
TestThread
::TestThread(QObject * parent
) : MW
(parent
){
isStopped = false;
isStopping = false;
}
void TestThread::stop()
{
isStopping = true;
}
void TestThread::process()
{
while (!isStopping)
{
while (!isStopping);
}
isStopped = true;
}
// mainwindow.h ====================================================================
void MainWindow::on_pushButton_Create_clicked()
{
pth = new TestThread(this);
clientthread = new QThread(this);
pth->moveToThread(clientthread);
//connect(clientthread, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCT(QObject* )));
//connect(CT, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCST(QObject* )));
connect(clientthread, SIGNAL(started()), pth, SLOT(process()));
//connect(CT, SIGNAL(finished()), clientthread, SLOT(quit()));
//connect(CT, SIGNAL(finished()), CT, SLOT(deleteLater()));
//connect(clientthread, SIGNAL(finished()), clientthread, SLOT(deleteLater()));
clientthread->start();
ui->pushButton_Create->setDisabled(true);
ui->pushButton_Delete->setEnabled(true);
}
void MainWindow::on_pushButton_Delete_clicked()
{
pth->stop();
while(!pth->isStopped);
clientthread->terminate();
//while(!clientthread->isRunning()); // why doesn't work ???
delete pth;
delete clientthread;
ui->pushButton_Create->setEnabled(true);
ui->pushButton_Delete->setDisabled(true);
}
// thread.h ===========================================================================
class TestThread : public QObject
{
Q_OBJECT
public:
explicit TestThread(QObject * parent);
volatile bool isStopped;
private:
volatile bool isStopping;
QObject * MW;
public slots:
void process();
void stop();
};
// thread.cpp =====================================================================
TestThread::TestThread(QObject * parent) : MW(parent)
{
isStopped = false;
isStopping = false;
}
void TestThread::stop()
{
isStopping = true;
}
void TestThread::process()
{
QString infostr = "hello !!!";
while (!isStopping)
{
while (!isStopping);
QCoreApplication::postEvent(MW,new TestEvent(TestEventDebug,""));
}
isStopped = true;
}
To copy to clipboard, switch view to plain text mode
It seems problem is solved but why doesn't work "while(!clientthread->isRunning());" ???
Bookmarks