Solved GUI is Freeze When Start QtConcurrent::run Process
-
Hi,
Please show the code where you are using QtCouncurrent.
If something freezes, you have likely used a method to wait on the result of your computation.
-
Thanks For Reply
void MainWindow::StartFunctions() { if(!future_1.isRunning()) future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); if(!future_2 .isRunning()) future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); } void MainWindow::Function_1() { while(IS_Thread_1_Start == true) { ///// My Code } } void MainWindow::Function_2() { while(IS_Thread_2_Start == true) { ///// My Code } } void MainWindow::on_BtnStart_clicked() //// START { StartFunctions(); } void MainWindow::on_BtnStop_clicked() //// STOP { IS_Thread_1_Start = false; IS_Thread_2_Start = false; }
-
Why are you implementing such blocking loops within your MainWindow code ?
-
Thanks For Reply
because I want to call my MainWindow Class functions in my Threads Functions
Like this::
void MainWindow::Function_1_Process() { ui->Label_1->setText(QString::number(Number_1)) /// Here Number_1 Is My Windows Class Member } void MainWindow::Function_2_Process() { ui->Label_2->setText(QString::number(Number_2)) /// Here Number_2 Is My Windows Class Member } void MainWindow::Function_1() { while(IS_Thread_1_Start == true) { Function_1_Process(); Number_1 += 1; } } void MainWindow::Function_2() { while(IS_Thread_2_Start == true) { Function_2_Process(); Number_1 += 1; } }
-
@Ketan__Patel__0011 said in GUI is Freeze When Start QtConcurrent::run Process:
because I want to call my MainWindow Class functions in my Threads Functions
Never do this directly! Only GUI thread is allowed to change the UI (access UI related objects)!
Emit a signal from the other thread and connect that signal to a slot in MainWindow. -
@jsulm
Thanks For ReplyI Understand
but now is there any other way to help for this problem ? -
@Ketan__Patel__0011 What is wrong with my suggestion?
You could also use https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod with Qt::QueuedConnection -
Your Suggestion Is Not Wrong
-
This post is deleted! -
@Ketan__Patel__0011 OK. Then take a look at the alternative I proposed.