Results 1 to 4 of 4

Thread: QT Thread termination Pls help

  1. #1
    Join Date
    Jul 2011
    Posts
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default QT Thread termination Pls help

    Hi all,

    I am new to QT programming. I have an application which consists of a thread for file write function which takes time. The user has option also to cancel the file write.I have implemented the cancel button using QT thread::terminate for stopping the file write,but Iam getting segmentation fault when i press the cancel button.Can anyone comment regarding this,is there any other option other than thread termination,pls help.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QT Thread termination Pls help

    You could use exit() or stop() for stopping a thread. To comment on your problem you may need to provide some more information. Please post a sample of your thread (or details like, does it use even loop, signals, slots, thread ownership, thread children etc..), and how exactly you are starting the thread.

  3. #3
    Join Date
    Jul 2011
    Posts
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QT Thread termination Pls help

    Pls see my code snippet below,I tried exit() also but it gives segfault

    WriteThread::run()
    {
    Fwrite();//file write function takes time to complete
    }

    Main::Cancel_clicked
    {
    WriteThread->terminate () ;
    WriteThread->wait();
    }

    Main::Write_clicked()
    {
    WriteThread-> start();
    }

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QT Thread termination Pls help

    terminating Fwrite() abruptly is not a good idea, it would be better to write chunks of data to file, and between writing chunks check for the cancel from the user, and when cancel is detected, you can close the handles properly and exit the thread, this way it will safe. terminating Fwrite() abruptly, may not be safe.

    Also there is somethings you can check for, are there any handles in Fwrite() which use else were?

    I would recommend to have a event loop in the thread, and you start a timer for writing chunks into file, this way you can send a cancel write signal to thread, and thread will clean up properly and exit / close itself.

    moreover you can read the terminate() documentation, and understand what would go wrong

Similar Threads

  1. Unsual Runtime Termination
    By ShamusVW in forum Qt Programming
    Replies: 15
    Last Post: 5th February 2010, 17:03
  2. Replies: 16
    Last Post: 7th October 2009, 08:17
  3. scope of dynamic variabe after program termination
    By babu198649 in forum General Programming
    Replies: 3
    Last Post: 6th February 2008, 13:09

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.