I have a background/loop script. If I trigger it once and issue the "Stop All Running Commands" command from the SF menu or keyboard command one error message is displayed via notifications.
I noticed that if I trigger the background script multiple times - let's say three times for example - and I stop all running commands then I get three error notifications which leads me to believe that there are multiple instances of the background script running.
Is this right and if so is there a way to prevent this? I've tried using a globalState
at the top of the script to prevent multiple instances running like this:
// Prevent multiple instances of this background process from running
// If this the first time the script has been launched then globalState.backgroundScriptRunning is undefined
if (globalState.backgroundScriptRunning == true) { throw 0 };
// Now set globalState.backgroundScriptRunning to true
globalState.backgroundScriptRunning = true
This does work but if I stop all running commands then globalState.backgroundScriptRunning
will be set to true
and will prevent the script from running again.
Is there another way to do this?
//CS//
- Christian Scheuer @chrscheuer2021-03-01 19:11:59.918Z
Hi Chris,
It's likely that you're using a modified version of the original runForever script I wrote. The original takes care of this exact issue so that you'll never see multiple scripts running at the same time.
Christian Scheuer @chrscheuer2021-03-01 19:12:35.744Z
Try to share the full script (minus the main function, you can leave that out)
Chris Shaw @Chris_Shaw2021-03-01 19:42:45.166Z