No internet connection
  1. Home
  2. How to

How to control/pause/stop a specific runForever script.

By N Leyers @NickLeyers_old_acc
    2020-05-29 05:15:40.132Z

    How can I script (or macro) to stop a specific command (local ID). Something that's running in the loop in the background?
    I don't want to stop all of them with the stop all command.

    Solved in post #6, click to view
    • 6 replies
    1. Hi Nick,

      You would set something like this:
      globalState.stopTheLoop = true

      And then in the loop check for something like:
      if (globalState.stopTheLoop) break;

      1. NN Leyers @NickLeyers_old_acc
          2020-05-29 08:50:06.057Z

          This will find all the running loops in all of the commands? The command that triggers the stop should find the command thats running in the background...
          To clarify: I have the script running that follows the output window on every channel I'm on. Handy, but it triggers my controll-app (dock and ipad) to go to the pan view all the time. I don't want that because I would like to stay in EQ-mode or sends-mode depending on what I'm doing.

          1. You have to think about it the other way round. Instead of programmatically "controlling" the script that you want to stop, you set a global "flag" to stop that other script.
            So, all the loops you want to control this way you'd need to change the code of.
            If you paste some code it's gonna be easier to show you.

            1. NN Leyers @NickLeyers_old_acc
                2020-06-01 06:53:10.520Z2020-06-01 09:48:18.363Z

                Hmm, I'm not following you here I'm affraid. But I understand that when this is running, it's not possible to intercept it because it's not just a running command anymore that you can turn on/off.

                This is the code:

                var lastFocusedTrackName;
                
                function main() {
                    try {
                
                        var newName = sf.ui.proTools.selectedTrackNames[0];
                        if (newName === lastFocusedTrackName || newName === undefined) return;
                
                        lastFocusedTrackName = newName;
                
                        if (sf.ui.proTools.selectedTrack.outputWindowButton.value.invalidate().value !== 'open')
                            sf.ui.proTools.selectedTrack.trackOutputToggleShow({ onError: 'Continue' });
                
                    } catch (err) {
                    }
                }
                
                function runForever(name, action, interval, timeout) {
                    var now = (new Date).valueOf();
                    if (now - globalState[name] < timeout) throw 0; //Exit if we were invoked again inside the timeout
                
                    globalState[name] = now;
                    sf.engine.runInBackground(function () {
                        try {
                            while (true) {
                                sf.engine.checkForCancellation();
                                globalState[name] = (new Date).valueOf();
                
                                action();
                
                                sf.wait({ intervalMs: interval, executionMode: 'Background' });
                            }
                        } finally {
                            globalState[name] = null;
                        }
                    });
                }
                
                runForever("isFollowFocusedTrackRunning", main, 500, 5000);
                
                1. Cool. You could change this to:

                  var lastFocusedTrackName;
                  
                  function main() {
                      try {
                          if (globalState.isFollowFocusedTrackPaused) return;
                  
                          var newName = sf.ui.proTools.selectedTrackNames[0];
                          if (newName === lastFocusedTrackName || newName === undefined) return;
                  
                          lastFocusedTrackName = newName;
                  
                          if (sf.ui.proTools.selectedTrack.outputWindowButton.value.invalidate().value !== 'open')
                              sf.ui.proTools.selectedTrack.trackOutputToggleShow({ onError: 'Continue' });
                  
                      } catch (err) {
                      }
                  }
                  
                  function runForever(name, action, interval, timeout) {
                      var now = (new Date).valueOf();
                      if (now - globalState[name] < timeout) throw 0; //Exit if we were invoked again inside the timeout
                  
                      globalState[name] = now;
                      sf.engine.runInBackground(function () {
                          try {
                              while (true) {
                                  sf.engine.checkForCancellation();
                                  globalState[name] = (new Date).valueOf();
                  
                                  action();
                  
                                  sf.wait({ intervalMs: interval, executionMode: 'Background' });
                              }
                          } finally {
                              globalState[name] = null;
                          }
                      });
                  }
                  
                  runForever("isFollowFocusedTrackRunning", main, 500, 5000);
                  

                  And then when you want to control whether or not it should be active, just set:

                  globalState.isFollowFocusedTrackPaused = true;
                  

                  to pause it, and this to unpause it:

                  globalState.isFollowFocusedTrackPaused = false;
                  
                  Reply1 LikeSolution
                  1. NN Leyers @NickLeyers_old_acc
                      2020-06-03 10:52:51.418Z

                      Perfect! Works as it should be :-)