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. Change the variables in a running function from the outside
QtWS25 Last Chance

Change the variables in a running function from the outside

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 993 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.
  • LimerL Offline
    LimerL Offline
    Limer
    wrote on last edited by Limer
    #1

    There's an interface, and when you click the start button, the start function will execute, and you can see that as long as paused is always false,the start function will go on forever.

    Because I add a CoreApplication:: processEvents (), so now the interface is still responsive. When I click the pause button, the pause function will be called, and then start will exit. I tried this way, and there was nothing wrong with it, but I'm not sure whether there are other dangers that I hadn't seen.

    I think if this way is feasible, it's unnecessary to use multi-thread.

    int n =1;
    
    void start()
    {
        while (1)
        {
            if (paused)
                return;
    
            qDebug() << n++;
    
            QElapsedTimer timer;
            timer.start();
            while (timer.elapsed() <= 500)
                QCoreApplication::processEvents(); // kepp the interface responsive
        }
    }
    
    void pause()
    {
        paused = true;
    }
    
    jsulmJ 1 Reply Last reply
    0
    • LimerL Limer

      There's an interface, and when you click the start button, the start function will execute, and you can see that as long as paused is always false,the start function will go on forever.

      Because I add a CoreApplication:: processEvents (), so now the interface is still responsive. When I click the pause button, the pause function will be called, and then start will exit. I tried this way, and there was nothing wrong with it, but I'm not sure whether there are other dangers that I hadn't seen.

      I think if this way is feasible, it's unnecessary to use multi-thread.

      int n =1;
      
      void start()
      {
          while (1)
          {
              if (paused)
                  return;
      
              qDebug() << n++;
      
              QElapsedTimer timer;
              timer.start();
              while (timer.elapsed() <= 500)
                  QCoreApplication::processEvents(); // kepp the interface responsive
          }
      }
      
      void pause()
      {
          paused = true;
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Limer Well, you can do it this way, but using processEvents() is considered bad design. Depending on what you're doing inside the loop it can be that you are not calling processEvents() often enough to keep your UI responsive. Also, keep in mind that with this approach other slots can be called while start() is running, so you can still have parallel access to variables (not at the same time though). It is still better to move long lasting operations in a thread.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      LimerL 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Limer Well, you can do it this way, but using processEvents() is considered bad design. Depending on what you're doing inside the loop it can be that you are not calling processEvents() often enough to keep your UI responsive. Also, keep in mind that with this approach other slots can be called while start() is running, so you can still have parallel access to variables (not at the same time though). It is still better to move long lasting operations in a thread.

        LimerL Offline
        LimerL Offline
        Limer
        wrote on last edited by Limer
        #3

        @jsulm Sorry, I forgot to add a while. The code above has changed.

        JonBJ 1 Reply Last reply
        0
        • LimerL Limer

          @jsulm Sorry, I forgot to add a while. The code above has changed.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Limer
          This is an "odd" way to arrange things. I'm not even sure it will do things the way you want.

          If the n++ represents "doing your other work", note that it won't be executing once the while (timer.elapsed() ... is executing. It executes once, then it does nothing (other than keep the UI alive) for half a second. If the user does press "Pause" during that time, it continues doing "nothing" till the rest of the half second has elapsed, only then does it exit start(). (You might want to change the loop to while (timer.elapsed() <= 500 && !paused)

          1 Reply Last reply
          2
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @Limer as mentioned in previous posts, you're approach isn't seem a good way to go. One advantage of using Qt framework is dealing with signals & slots to avoid dealing with event loops directly.

            Given that said, you may find this example about a file downloader interesting regarding the way the cancel action (pause in your case) is handled to stop (pause) an ongoing download.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            LimerL 1 Reply Last reply
            1
            • Pablo J. RoginaP Pablo J. Rogina

              @Limer as mentioned in previous posts, you're approach isn't seem a good way to go. One advantage of using Qt framework is dealing with signals & slots to avoid dealing with event loops directly.

              Given that said, you may find this example about a file downloader interesting regarding the way the cancel action (pause in your case) is handled to stop (pause) an ongoing download.

              LimerL Offline
              LimerL Offline
              Limer
              wrote on last edited by
              #6

              @Pablo-J.-Rogina Thanks for your response.

              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