Results 1 to 2 of 2

Thread: how to terminate a thread when it is running

  1. #1
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to terminate a thread when it is running

    i use the terminate function in the MainWindow's destruction function. i have add the setTerminationEnabled(true); in the thread's construction function. at the mainwindow's destruction function i invoke it like this: thread->terminate(); but when i close the applicaiton the gui is closed and the application process is still running. .. the thread is blocking waiting client's data. i search the froum and try the solution that i found. my debug log show that it is blocking at the terminate() fucntion. my question is: how to terminate the thread when it is running? any help is appreciate!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to terminate a thread when it is running

    In general it is better if you can show the code you are talking about.
    Also, it is not a good practice to terminate a thread from outside the thread the way you are doing it.
    It is better to set some flag in the thread class, and allow the thread to terminate it self.
    You can then connect the finished() or terminated() signals to slots out side the thread to know your thread has terminated, and then continue to close your application.

    Also note, the you calling terminate() does not guarantee that the thread indeed is terminated when your window destructor continues, and possibly even finished by its self, before the thread is terminated.
    Have a look at doc of QThread.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 5
    Last Post: 17th January 2008, 21:49
  2. running() - Thread
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2007, 18:45
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 06:27
  5. How to destroy or terminate a thread ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 26th December 2006, 16:44

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.