Results 1 to 2 of 2

Thread: Best way to terminate a copy thread?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Best way to terminate a copy thread?

    I have a common scenario: a dialog show the progress of copying lots of files and has a cancel button. The copy process is taking place in a QThread class which fires signals as files are copied and exit's the run() method when finished. the dialog gets the finished signal and closes itself.

    Question: What is the best way for the dialog to shutdown the thread if the cancel button is clicked? In the old days I would simply have the dialog set a property on the thread class that the thread class would check after copying each file. If it was set, exit. Is there a better way to do this in Qt? I ask because there are other ways of doing it in .Net.

    Sam

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Best way to terminate a copy thread?

    I assume you have an event loop in your thread (you need to to deal with events and signals and slots).
    Then you can use quit() to exit the event loop. But before quiting, stop the copy process gracefully. Either proceed with the current file till it is copied and then stop for the rest of the files and quit. Or stop copying immediatly and delete the part of the copied file before quiting the event loop.
    When a thread is finished, it emits the finished signal.

    You might want to add a slot to do all this and connect it to the cancel button.

    Beware of using signals and slots in threads.
    http://labs.trolltech.com/blogs/2010...oing-it-wrong/

Similar Threads

  1. how to terminate a thread when it is running
    By guchangyuan in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2009, 10:50
  2. Unable to terminate QCoreApplication
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 3rd February 2009, 10:54
  3. QThread::terminate () is not working!!!!!
    By biswajithit in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2008, 12:03
  4. How to terminate NULL character ?
    By merry in forum Qt Programming
    Replies: 3
    Last Post: 8th July 2008, 20:01
  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
  •  
Qt is a trademark of The Qt Company.