PDA

View Full Version : PostEvent



user_mail07
31st May 2007, 23:06
I am trying to post event from a thread using postEvent() method and that should send a "custom event " to the event loop. But Somehow postEvent is not working correctly and it never called customEvent.



Any idea will be helpful.


///////Header File////////

class CustomThreadEvent : public QEvent
{
public:
CustomThreadEvent()
: QEvent( (QEvent::User + 150)) {}

QThread *pErase;
};


/////METHOD////////////

void MyThreadEvent::EraseThread( QThread *pErase)
{


CustomThreadEvent *pCustomThreadEvent = new CustomThreadEvent;

if ( pCustomThreadEvent)
{
pCustomThreadEvent->pErase = pErase;

//Calling Post Event

QApplication::postEvent( this, pCustomThreadEvent );

}
}

jacek
31st May 2007, 23:17
What MyThreadEvent class does exactly? How did you implement MyThreadEvent::customEvent()? Maybe you should use QObject::deleteLater() instead?

Also, please, use [code] tags for code snippets.

marcel
31st May 2007, 23:30
Is MyThreadEvent a QThread?
Because if it is, then you have to call exec to start the event loop and be able to receive events.

I see that you post that event in the same class's event queue. What is the point for that?
It should be at least across threads.

And why use custom events at all when you have signals/slots and queued connections.

And about deleting a thread like that!...
If you have the receoier object, why don't you just connect the thread's finished signal to a slot in that receiver, and delete it, or whatever.

Regards

user_mail07
31st May 2007, 23:33
MyThreadEvent is inherited from QObject and basically it deletes the threads that post a message to this class.



1 void MyThreadEvent ::customEvent( QEvent *e )
2. {
3. if ( e->type() == QEvent::User + 150 )
4. {
5. CustomThreadEvent *pCustomThreadEvent = ( CustomThreadEvent *) e;

6. if ( pCustomThreadEvent )
7. {
8. EraseThread( pCustomThreadEvent->pErase );
9. }
10. }
11.}


What EraseThread() is doing it will wait until thread is finished executing and then it will delete the thread i.e pErase. using delete pErase.

Sorry for the tags that i was not aware of.

jacek
31st May 2007, 23:38
MyThreadEvent is inherited from QObject and basically it deletes the threads that post a message to this class.
You can do the same by simply calling deleteLater().

Where does that MyThreadEvent object live? Because, as Marcel has noted, you have to start the event loop to process events sent to non-GUI threads.

marcel
31st May 2007, 23:41
What EraseThread() is doing it will wait until thread is finished executing and then it will delete the thread i.e pErase. using delete pErase.


Do you sleep in EraseThread?
It is recommended hat you use the finished signal, because you are risking to block the thread in which MyThreadEvent lives by waiting, since it can take some time for a thread to finish.

Regards

user_mail07
31st May 2007, 23:48
I have not used sleep but I am using wait() for terminating that thread.
Here the implementation of Erase Thread.

if ( pErase )
{

if ( pErase->wait( millisecsWait ) == FALSE )
{
//Kill it if it is still running
pErase->terminate();


pErase->wait();
}

// delete it.
delete pErase;

marcel
31st May 2007, 23:53
Hey, but that is what wait does. It actually waits some time, does not return immediately.
And terminate() is not that safe to use regularly.

Why not use volatile bool flag(s) in the thread(s), that you just set to true when you want a thread to exit run()? And in run() you test for that flag in all time consuming operations( loops, etc ).

Or if you have run exec() in the worker threads, just make sure you call exit, to make them exit the event loop, therefore run( do this with a custom event, signal, etc... ).

But the bottom line is that wait also wastes some time so you should think( and read the docs about it) before using it.

Regards

marcel
1st June 2007, 00:01
Anyway, about customEvent() not getting called: does the thread in which the MyThreadEvent lives have an event loop?

Because you can solve it by starting an event loop. Then you can get convinced that you will encounter some delays when deleting threads like that.

Regards