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. Stop a function with "Stop" button

Stop a function with "Stop" button

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.4k Views
  • 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.
  • V Offline
    V Offline
    valeSimu
    wrote on last edited by
    #1

    I have a function that runs some calculations and I would like to allow user to stop it with a button.
    In the interface there are two simple button: Start and Stop.

    I don't know exactely what do you need as code, the function is very long... let me know!

    Thank you

    1 Reply Last reply
    0
    • T Offline
      T Offline
      topse
      wrote on last edited by
      #2

      Mmh... I assume, you are running your calculations in your GUI-Thread? (Or did you explicitly create a worker thread?)

      If that is true, your application can't process GUI-Events as long as the calculation is in progress. You can force your GUI-Thread to process events, if you do the following function within your loop:

      QCoreApplication::processEvents();

      If you do so, your application should first become "kind-of-responsive" again. [Better would be to create a worker thread for your calculation!]

      Next step is, that the Stop-Button sets a flag, that it has been pressed. Then you check this flag within your calculation loop and if it is set, you break out of the loop and stop the calculation.

      I hope this helps!

      Best Regards,
      Tobias

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on last edited by
        #3

        In my opinion the question is too vague. Have considered the option to do the calculations in a separate thread using "QThread":https://qt-project.org/doc/qt-5/qthread.html? You can bind a signal from your GUI with a slot of the thread and to terminate it.

        http://anavi.org/

        1 Reply Last reply
        0
        • T Offline
          T Offline
          topse
          wrote on last edited by
          #4

          I have a rule of thumb: "Never use Terminate-Thread!"

          Reason is simple: you never know, when exactly i gets terminated, so application state after termination may be undefined, for example: what happens if you terminate the thread while it is inside a Critical Section? It will never leave it... and the critical section will be blocked forever...

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #5

            [quote author="topse" date="1398855116"]I have a rule of thumb: "Never use Terminate-Thread!"

            Reason is simple: you never know, when exactly i gets terminated, so application state after termination may be undefined, for example: what happens if you terminate the thread while it is inside a Critical Section? It will never leave it... and the critical section will be blocked forever...[/quote]

            As I said before the question is too vague. The solution depends on the exact situation.
            Here a good article about "Keeping the GUI Responsive":http://doc.qt.digia.com/qq/qq27-responsive-guis.html with Qt. As you can see several approaches are described, including a way using a worker thread. There a lot of well known paradigms to escape from deadlocks and to handle critical sections and resource in multithreaded applications.

            http://anavi.org/

            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