PDA

View Full Version : Thread Help



tntcoda
16th January 2009, 22:05
Hi,

Im having some problems with cross thread communication, basically i get an assert error:



"ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 72fc768. Receiver 'ProDialog' (of type 'progressDlg') was created in thread 50d4058", file kernel\qcoreapplication.cpp, line 296"


Im using signals/slots to send data from the new thread to the ProDialog instance, and im not sure why it doesn't like it :confused the only shared object is the container instance, i havent bothered locking/unlocking this as I know its not in use while the thread is executing, hopefully thats good enough.

Heres some code extracts:

Main window code:


// Create progress dialog
pro = new progressDlg();
pro->show();

// Spawn a new worker thread
enc = new TEnc;

// Connect signals/slots
connect(enc, SIGNAL(StartingFile(QString)), pro, SLOT(setProgressLabel(const QString&)), Qt::DirectConnection);
connect(enc, SIGNAL(AddFilesDone(bool)), this, SLOT(AddFilesComplete(bool)));
connect(enc, SIGNAL(Progress(int, int)), pro, SLOT(setProgress(int, int)), Qt::DirectConnection);

enc->setParams(container, files);
enc->start(); // start the thread



The TEnc object (QThread subclass)


void TEnc::run()
{
emit Progress(files.size(), 0);
QStringList bits;

// do the work
for(int i = 0; i < files.size(); i++)
{
bits = files.at(i).split("/"); // extract file name from path
if(bits.size() == 0)
bits = files.at(i).split("\\");

if(bits.size() != 0)
emit StartingFile("[" + QString::number(i+1) + "/" + QString::number(files.size()) + "] " + bits.at(bits.size() - 1)); // send signal out that we are starting

if(!container->AddItem(files.at(i).toAscii(), NULL))
{
emit AddFilesDone(false); // fail with error
return;
}

emit Progress(files.size(), i + 1); // update progress
}

// all done signal
emit AddFilesDone(true); // singal -> all files done
}


Any ideas what silly mistake ive made this time :o

Thanks,

Jack

jpn
16th January 2009, 22:10
Removed the Qt::DirectConnection parameter. You don't want to invoke direct calls from the worker thread to the GUI thread.