Can not Start multiple Thread
-
wrote on 14 Sept 2020, 07:33 last edited by Ketan__Patel__0011
Hello friend currently i am working on parallel processing concepts
i want run my 2 or 3 functions parallelly
when i start Thread Number 1 on my button click event that time Thread one is started but after thread number 1 is started i can't perform any other operation in my application until Thread number 1 process is not finished
My Code is :
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); TestingThread_1 = new QThread(); connect(TestingThread_1,SIGNAL(started()),this,SLOT(Start_Processing_1())); TestingThread_2 = new QThread(); connect(TestingThread_2,SIGNAL(started()),this,SLOT(Start_Processing_2())); TestingThread_3 = new QThread(); connect(TestingThread_3,SIGNAL(started()),this,SLOT(Start_Processing_3())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::Start_Processing_1() { for(i = 0 ; i < 100 ; i++) { ui->label->setText(QString::number(i + 1)); QThread::msleep(20); qDebug() << i; } } void MainWindow::Start_Processing_2() { for(int i = 0 ; i < 100 ; i++) { ui->label_2->setText(QString::number(i + 1)); QThread::msleep(20); qDebug() << i; } } void MainWindow::Start_Processing_3() { for(int i = 0 ; i < 100 ; i++) { ui->label_3->setText(QString::number(i + 1)); QThread::msleep(20); qDebug() << i; } } void MainWindow::on_pushButton_clicked() { TestingThread_1->start(); } void MainWindow::on_pushButton_2_clicked() { TestingThread_2->start(); } void MainWindow::on_pushButton_3_clicked() { TestingThread_3->start(); }
please friends help me to solve this problem
Thanks in Advance -
The methods
Start_Processing_1()
,Start_Processing_2()
andStart_Processing_3()
are run in your main thread. YourQThread
instances do nothing.Please read and use the docs, the way QThread works is described in detail there. https://doc.qt.io/qt-5/qthread.html
In short:
- either subclass QThread and reimplement
run()
- or use worker object approach
- either subclass QThread and reimplement
1/2