PDA

View Full Version : Thread not having anytime?



steg90
28th September 2007, 09:43
Hi,

I have some code which creates a thread which basically just emits a signal back to the worker thread which puts a string into a QListWidget. Problem is, the items in the list widget are not shown until the worker thread has finished what it was doing?

This is some of the code :



void CInitialiseThread::run()
{
while( !m_bStopped )
{
emit AddInfo();
msleep( 10 );
}
}


Thread is created in the following function :



void CanView::LoadupDetails()
{
if( theApp->m_pWorkspace )
{

m_pCanInitialiseThread = new CInitialiseThread();

connect( m_pCanInitialiseThread, SIGNAL(AddInfo()), this,
SLOT(AddInfo()) );

if( !m_pCanInitialiseThread->isRunning() )
{
m_pCanInitialiseThread->start();
}

QList<CCanChannel*>*pcanFilterObj = &theApp->m_pWorkspace->m_oCanChannels;
if( pcanFilterObj )
{
for( int i = 0; i < pcanFilterObj->count(); i++ )
{
CCanChannel* pCanChannel = (CCanChannel*)pcanFilterObj->at( i );
if( pCanChannel )
{
int nChannel = ui.canBox->currentIndex() + 1;
QString strId;
strId.sprintf( "%i", nChannel );
if( pCanChannel->m_strCanChannel == strId )
{
// found channel we want
int nBaud = ui.baudBox->findText( pCanChannel->m_strBaud );
ui.baudBox->setCurrentIndex( nBaud );
if( pCanChannel->m_pCanFilter )
{
// set extended flag and can id flag
//ui.Extended->setChecked( pCanChannel->m_pCanFilter->m_bExtended );
//ui.canIdCheck->setChecked( pCanChannel->m_pCanFilter->m_bFilterOnId );

//ui.canIdList->clear();

QString strId = "";
// any filtering on canid's?
for( int j = 0; j < pCanChannel->m_oFilterIdList.count(); j++ )
{
// ui.canIdList->addItem( pCanChannel->m_oFilterIdList.at(j) );
int nHex = pCanChannel->m_oFilterIdList.at(j).toInt( 0, 16 );
strId.sprintf( "%x", nHex );
theApp->AddFilterByCanId( strId, theApp->GetChannelID()+1 );//1 );
}


CCanFilterDetails* pCanDetails;
for( int jj = 0; jj < pCanChannel->m_pCanFilter->m_oCanFilterDetails.count(); jj++ )
{
pCanDetails = (CCanFilterDetails*)pCanChannel->m_pCanFilter->m_oCanFilterDetails.at(jj);

// Configure can - does passthruconnect for each filter
long lFlags = 0;
bool ok;
int iMask = pCanDetails->m_strMask.toLong( &ok, 16 );
int iPattern = pCanDetails->m_strAccept.toLong( &ok, 16 );

if( pCanChannel->m_pCanFilter->m_bExtended )
lFlags = 0x100;

QString strFilterType = pCanDetails->m_strType;
if( pCanChannel->m_pCanFilter->m_bExtended )
lFlags = 0x100;

QString strCanChannel = pCanChannel->m_strCanChannel;// ui.canChannels->currentText();
int nBaud = pCanChannel->m_strBaud.toInt();//ui.baudRate->currentText().toInt();
int iReply = theApp->ConfigureCan( nBaud, nBaud, strCanChannel ); // configre can first

unsigned long iFilterId = SetFilter( pCanDetails->m_strMask, pCanDetails->m_strAccept, lFlags, 1, TRUE );


}
}

break;
}
}

}
}
}
if( m_pCanInitialiseThread->isRunning() )
{
m_pCanInitialiseThread->stop();
m_pCanInitialiseThread->wait();
}
}


The Addinfo basically just adds a QString to a QListWidget. I don't know why the QListWidget isn't being updated every 10ms?

Guess I'm doing something wrong or there is no CPU time?

Regards,
Steve

wysota
28th September 2007, 10:02
You are not letting the event loop deliver the signal. You just have a long block of code where you start a thread then do something and then stop the thread. And you should start the thread, do something, return to the event loop (at this point signals will start to be delivered to your main thread) and when the worker thread signals that it is done doing its job, call stop and wait. So at least you should put the stop() and wait() calls into another slot connected to one of the thread's signals.

steg90
28th September 2007, 10:15
Hi,

The signal seems to be getting delivered, it seems it is the QListWidget not updating - I have since put in QCoreApplication::processEvents() and this seems to help. I thought I could just start the thread and it would run and the signal would be delivered. I see what you are saying, the thread emits the signal, but the event loop cannot process it due to still running my other block of code.

Regards,
Steve

wysota
28th September 2007, 10:39
By "delivered" I meant "processed by the receiver". Maybe I should have said it more clearly, sorry :)

steg90
28th September 2007, 11:07
Lol. Thanks for your help, always appreciated ;)

Steve