Results 1 to 5 of 5

Thread: Thread not exiting

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Thread not exiting

    Hi,

    I have an application which starts a thread ( QThread ), to stop the thread I have a push button in the main gui, when pressed I want it to stop this thread, I thought I could simply do :

    Qt Code:
    1. m_pMyThread->quit();
    To copy to clipboard, switch view to plain text mode 

    But alas, the thread carries on running?

    Any ideas?

    Regards,
    Steve

  2. #2
    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: Thread not exiting

    Did you run exec() in QThread::run()?
    If not, this function has no effect, because it tells the thread's event loop to exit with code 0, but in this case the thread doesn't have an event loop.

    Could we see your run method?
    You might wanna add a mCancelled member in your thread, and a function cancelThread(). Inititalliy mCancelled is false and is set true by cancelThread.

    You call cancelThread instead of quit. Make sure to test mCancelled in the loops in your run method.

    regards

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread not exiting

    Hi,

    I don't call exec() from my run method, here it is :

    Qt Code:
    1. void CanRead::run()
    2. {
    3. m_nCount = 0;
    4.  
    5. unsigned long nMsgAmount = 2;
    6.  
    7. CANDATA can[MAX];
    8.  
    9. QString szTemp;
    10.  
    11. while( true )
    12. {
    13. #ifdef __CANON_
    14. unsigned long numMsgs = 0;
    15. PASSTHRU_MSG *pRxMsg = NULL;
    16. pRxMsg = theApp->GetMessageDetail( &numMsgs, 0 );
    17. nMsgAmount = numMsgs; // store amount of messages we got
    18. int index = 0;
    19. // Read can data, store in buffer ( read say N amount and then emit? )
    20. while ( numMsgs )
    21. {
    22. unsigned int iCanId = 0;
    23. for ( int iLoop = 0; iLoop < 4; iLoop++)
    24. {
    25. iCanId = (iCanId << 8 ) | pRxMsg->Data[iLoop];
    26. }
    27. if( pRxMsg->RxStatus & 0x100 )
    28. can[index].strId.sprintf( "%X X", iCanId);
    29. else
    30. can[index].strId.sprintf( "%03X", iCanId);
    31.  
    32. for ( int iLoop = 4; (unsigned)iLoop < (pRxMsg->DataSize ); iLoop++)
    33. {
    34. szTemp.sprintf("%02X ", pRxMsg->Data[iLoop]);
    35. can[index].strData += szTemp;
    36. }
    37.  
    38. can[index].strTime.sprintf("%04d", pRxMsg->Timestamp);
    39.  
    40. numMsgs--;
    41.  
    42. m_nCount++;
    43.  
    44. } // end while ( ulNoMsgs )
    45.  
    46. #else // some dummy data for testing table view
    47.  
    48. can[0].strId = "0xfea";
    49. can[0].strData = "222222222222";
    50. can[0].strTime = "----";
    51.  
    52. can[1].strId = "0xfea";
    53. can[1].strData = "111111111111";
    54. can[1].strTime = "----";
    55.  
    56. m_nCount += nMsgAmount;
    57.  
    58. #endif
    59. // send out signal
    60. if( nMsgAmount > 0 )
    61. emit scrolltable( m_nCount - 2, nMsgAmount, &can[0] );
    62. msleep(1);
    63.  
    64. } // end while ( true )
    65.  
    66. }
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Steve

  4. #4
    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: Thread not exiting

    Yes, I assumed you did so.
    Add the mCancelled member and replace the two "while" conditions with:

    1. The first while:
    Qt Code:
    1. while( !mCanceled )
    To copy to clipboard, switch view to plain text mode 
    2. The second while:
    Qt Code:
    1. while( numMsgs && !mCancelled )
    To copy to clipboard, switch view to plain text mode 
    Declare mCancelled in the thread class as ( you can make it private ):
    Qt Code:
    1. volatile bool mCancelled;
    To copy to clipboard, switch view to plain text mode 
    Also, add a new member function( public ):
    Qt Code:
    1. void cancelThread();
    2. ...
    3. void CanRead::cancelThread()
    4. {
    5. mCancelled = true;
    6. }
    To copy to clipboard, switch view to plain text mode 
    All you have to do next is to call cancelThread instead of quit().
    Note that it may take a little for the thread to stop, because it may be inside one of the loops when you set mCanceled to true. But this delay is acceptable, in my opinion, and you may not even notice it.

    EDIT - Don;t forget to make mCancelled false in the constructor of the thread

    Regards

  5. The following user says thank you to marcel for this useful post:

    steg90 (9th May 2007)

  6. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread not exiting

    Thanks Marcel, you are a great help.

    Kind regards,
    Steve

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  3. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  4. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44
  5. Replies: 2
    Last Post: 6th January 2006, 21: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.