Results 1 to 9 of 9

Thread: PostEvent

  1. #1
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default PostEvent

    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.
    Qt Code:
    1. ///////Header File////////
    2.  
    3. class CustomThreadEvent : public QEvent
    4. {
    5. public:
    6. CustomThreadEvent()
    7. : QEvent( (QEvent::User + 150)) {}
    8.  
    9. QThread *pErase;
    10. };
    11.  
    12.  
    13. /////METHOD////////////
    14.  
    15. void MyThreadEvent::EraseThread( QThread *pErase)
    16. {
    17.  
    18.  
    19. CustomThreadEvent *pCustomThreadEvent = new CustomThreadEvent;
    20.  
    21. if ( pCustomThreadEvent)
    22. {
    23. pCustomThreadEvent->pErase = pErase;
    24.  
    25. //Calling Post Event
    26.  
    27. QApplication::postEvent( this, pCustomThreadEvent );
    28.  
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 31st May 2007 at 23:19. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PostEvent

    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.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PostEvent

    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

  4. #4
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: PostEvent

    MyThreadEvent is inherited from QObject and basically it deletes the threads that post a message to this class.


    Qt Code:
    1. 1 void MyThreadEvent ::customEvent( QEvent *e )
    2. 2. {
    3. 3. if ( e->type() == QEvent::User + 150 )
    4. 4. {
    5. 5. CustomThreadEvent *pCustomThreadEvent = ( CustomThreadEvent *) e;
    6.  
    7. 6. if ( pCustomThreadEvent )
    8. 7. {
    9. 8. EraseThread( pCustomThreadEvent->pErase );
    10. 9. }
    11. 10. }
    12. 11.}
    To copy to clipboard, switch view to plain text mode 


    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.
    Last edited by user_mail07; 31st May 2007 at 23:35. Reason: missing [code] tags

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PostEvent

    Quote Originally Posted by user_mail07 View Post
    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.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PostEvent

    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

  7. #7
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: PostEvent

    I have not used sleep but I am using wait() for terminating that thread.
    Here the implementation of Erase Thread.
    Qt Code:
    1. if ( pErase )
    2. {
    3.  
    4. if ( pErase->wait( millisecsWait ) == FALSE )
    5. {
    6. //Kill it if it is still running
    7. pErase->terminate();
    8.  
    9.  
    10. pErase->wait();
    11. }
    12.  
    13. // delete it.
    14. delete pErase;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PostEvent

    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
    Last edited by marcel; 1st June 2007 at 00:23.

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PostEvent

    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

Similar Threads

  1. Replies: 4
    Last Post: 26th July 2006, 07:06
  2. postEvent() to a non-existing receiver
    By Artschi in forum Qt Programming
    Replies: 7
    Last Post: 1st June 2006, 14:44
  3. using postEvent, but still getting async error
    By ender in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 17:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.