Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Quit application from within thread
Forum Updated to NodeBB v4.3 + New Features

Quit application from within thread

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hellagot
    wrote on last edited by
    #1

    hi there, i have a application that is quite similar to the threaded fortune server.

    So what i have, is a QT application, with QMainWindow in it, that has a QTcpServer running, that starts threads on incomming connections.

    What i want to do, is, quit the whole application from within one of the threads started by the server.

    I tried different things:

    • just call application->quit();

    Says that a thread was killed while running, unclean exit

    • call QApplication::postEvent(application, new QCloseEvent()) from the end of the threads run() method

    and:

    • connect(thread, SIGNAL ( finished ) , application, SLOT ( quit ) );

    Exits unclean

    Any ideas how i could exit the application cleanly from within the thread?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      miroslav
      wrote on last edited by
      #2

      You need to add synchronization that waits for all threads to finished before main() exits. If you are sure (to some extend :-) ) that the threads will exit, you can call wait() on the threads that are still running before exiting main.
      The event loop (QApplication::exec()) will exit after processing all the events, and not wait for other threads to exit.

      Mirko Boehm | mirko@kde.org | KDE e.V.
      FSFE Fellow
      Qt Certified Specialist

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hellagot
        wrote on last edited by
        #3

        Thanx, actually i removed the threads and it turned out that it was some not closed object that was causing the unclean exit ;) but i will return to threads and its good to know

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anticross
          wrote on last edited by
          #4

          You can call terminate() to all your threads before you exit your program, by reimplementing close event on your main window and insert there a call to terminate all threads.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved