Results 1 to 5 of 5

Thread: Correct way to quit a console application

  1. #1
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Correct way to quit a console application

    Running a QT5.3 console application on OSX10.8.5. The application has a few threads working. However, when I CMD-Q the application the threads still keep on running. I have to kill the program in Activity Monitor. What is the correct way to exit a program including object deletion and thread quitting?

    Cheers

  2. #2
    Join Date
    Mar 2011
    Posts
    118
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Correct way to quit a console application

    Try this : Before quit the main thread try to quit all running thread and wait for the process to be finished(signal & slot / sleep).
    May be it will help you.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Correct way to quit a console application

    You need to
    1) tell all threads to stop
    2) let the main thread wait for the other threads to stop

    (1) depends on what the threads do, i.e. if the run an event loop or some processing loop.

    (2) can be done by either making the threads signal their exit, count that and quit the main thread when all threads have called in, or by waiting on each thread individually, or by sharing a semaphore between the main thread and the worker threads and have the main thread acquire worker thread count and then worker threads release 1 each.

    Cheers,
    _

  4. #4
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Correct way to quit a console application

    Not sure I'm doing this right. Here's some code:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. LSA listenagain;
    6.  
    7. QObject::connect(&a, SIGNAL(aboutToQuit()), &listenagain, SLOT(aboutToQuit()));
    8.  
    9. listenagain.start();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    The signal and slot seem to connect fine but then I can't close the other threads.

    Qt Code:
    1. QThread* thread = new QThread;
    2. showRecord * record = new showRecord(showName, fileName, startHour, startMin, endHour, endMin);
    3. record->moveToThread(thread);
    4. connect(record, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
    5. connect(thread, SIGNAL(started()), record, SLOT(process()));
    6. connect(record, SIGNAL(finished()), record, SLOT(deleteLater()));
    7. connect(record, SIGNAL(finished()), thread, SLOT(quit()));
    8. connect(this, SIGNAL(shuttingDown()), record, SLOT(stopRecording()));
    9. thread->start();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void LSA::aboutToQuit() {
    2. emit shuttingDown();
    3. m_timer.stop();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Not entirely sure how to delete the showRecord object.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,791
    Thanks
    263
    Thanked 776 Times in 766 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Correct way to quit a console application

    Not entirely sure how to delete the showRecord object.
    If you guarantee that the showRecord instance will not be referenced after it emits the finished() signal, then you should be able to call deleteLater() immediately after you emit the signal, wherever that is. The instance will be deleted next time the event loop runs.

    Alternatively, you can add another connect() to the finished() signal when you create the thread.

    Qt Code:
    1. connect(record, SIGNAL(finished()), record, SLOT(deleteLater()));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 1st August 2011, 07:30
  2. Replies: 1
    Last Post: 28th July 2011, 10:10
  3. Replies: 2
    Last Post: 21st November 2010, 19:03
  4. Application won't quit until event loop quits
    By caelestis in forum Qt Programming
    Replies: 6
    Last Post: 11th February 2010, 08:21
  5. Why my application quit?
    By yxtx1984 in forum Newbie
    Replies: 1
    Last Post: 7th February 2010, 06:46

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.