Pausing QThread on infinite loop

Viewed 1407

I have a Qt gui as QMainWindow which has a QThread that starts a infinite loop of the object of another class.

Here is the brief code: Calculations.h

Class Calculations : public QObject
{
public:
//many functions!
void run();
signals:
void signalResultAvailable();
void signalECUQueryTimePassed();
private:
//many things!
}

and Calculations.cpp

void Calculations::run()
{
    while (1)
    {
        unsigned long loopStart = millis();

        //Heavy calculations per second!

        unsigned long loopEnd = millis();
        if ((loopEnd - loopStart) <= 1000)
        {
                delay(1000 - (loopEnd - LoopStart));
                emit signalResultAvailable;
        }
     }
}

MainWindow.his as follows:

Class MainWindow : public QMainWindow
{

private slots:
    on_button_Pause_clicked();
private:
    Calculations cal;
}

MainWindow.cpp:

MainWindow::MainWIndow() : ui(new Ui::MainWindow), cal()
{
    //all the initializations...

    QThread *thread_1 = new Qthread;
    cal.moveToThread(thread_1);
    connect(thread_1, SIGNAL(started()), &cal, SLOT(run()));
    thread_1->start();
}

void MainWindow::on_buttonPause_clicked()
{
    // I want to put the Thread pause HERE!
}

I want to have a Pause button on the GUI, so when the user clicks it pauses the qthread (absolutely GUI must not freeze!) and when a user clicks the Resume button it continues the calculations. I've read this topic as well but it didnt help since it uses the QThread subclass as I understood.

Can anybody please help me?

2 Answers
Related