No internet connection
  1. Home
  2. How to

How to run a script in background and check for a state

By Christian Scheuer @chrscheuer2019-07-29 01:20:14.032Z2019-07-29 01:34:49.022Z

Sometimes you want to do something advanced with Soundflow that our triggers don't support yet.
For example waiting on a specific window to become available, or something else.
In those cases you'll want to run a command in "background" mode, that is a command that's running indefinitely while allowing other commands to run (ie. don't block with a blue icon).

Here's some sample code to get you started with that:


//How often to check in milliseconds
var pollingIntervalMs = 100;

//sf.engine.runInBackground means: Run in background so other commands may run while we're running
sf.engine.runInBackground(function () {

    //while(true) is an endless loop
    while (true) {
  
        //Check if anyone requested us to cancel all running commands every time we enter the loop
        //The action will take care of aborting this command if necessary
        sf.engine.checkForCancellation();

        //Here you insert your code you want to execute every 100ms (or whatever you set the pollingIntervalMs to)

        //Done with this iteration of the loop.
        //To not eat up all the CPU, insert a 'wait' here so we wait until next time we want to check
        sf.wait({
            intervalMs: pollingIntervalMs,
            executionMode: 'Background'
        });
    }

});
Solved in post #3, click to view
  • 1 replies
  1. In reply tochrscheuer:

    Solution in the question

    ReplySolution
    1. Progress
    2. @chrscheuer closed this topic 2019-07-29 01:26:54.790Z.
    3. @chrscheuer reopened this topic 2019-07-29 01:27:05.900Z.