Connection of signal from separate ( child ) thread to slot of mainwindow ( parent thread - main thread ) problem[SOLVED]
-
Hi,
How are you doing?
This is a relatively simple threading problem. I have a main thread where child thread were created and run called:
@@dcc = new DetectClassifyComputation();
connect(dcc,SIGNAL(data_ready()),this,SLOT(plot_processed_samples()),Qt::QueuedConnection);....
dcc->updatePDParameters(this->samples_to_process,&pks,&pks_ind,this->smpl_rate,stng.getUi()->spinBox_det_ratio->value()*0.1);
cout<<"before starting new thread "<<endl;
dcc->setStackSize(100000);
dcc->start(QThread::HighPriority);
cout<<"after starting new thread "<<endl;
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
this->setEnabled(false);
cout<<"PREPROCESS 6. "<<endl;
while(!dcc->isFinished())
{
QApplication::processEvents();
}
this->setEnabled(true);
QApplication::restoreOverrideCursor();
cout<<"END OF PREPROCESS "<<endl;@where dcc is the instance of the thread class. The signal is defined in the thread class as:
@Q_SIGNALS:
void data_ready();@
I observe, that MainWindow where I call the run of the thread "hangs" ( "freezes") after some time ( on third or second run of the thread ) with exception code c0000005. That is on Windows 7 and the program fails.
It seems once the slot can't be invoked.
My question is what is the problem? How to fix that?
Thank you in advance in any help with this issue. -
It is not quite clear from your explanation what happens... do you see your debug "after starting new thread"?
I just notice one thing, with every thread that I have ever used once it is set to running I never call functions directly on that thread. I always use signal/slot approach. It is little bit dangerous to call functions from other threads directly.
so in theory you would connect another signal/slot from dcc to a slot on "this" (main window?) telling it that it has finished doing whatever it is doing.
Also worth checking / debugging your dcc thread that it is still running (get some printfs or somthing comming out of it to see what it is doing).I assume you have some code in the Thread::run() function for when it gets the start signal? - what happens then?
-
Hi,
Thank you for your try to help. Yep, I connect signal from child thread with slot from parent thread. I've fixed recently the problem, the signal-slot connection should be queued, I've left default thread stack size and the
while(!dcc->isFinished())
{
QApplication::processEvents();
}
should not be run. So it works fine now. Closed. Thanks. -
no problem, well done, good luck :)