Jump to content

How to prevent two scripts from running concurrently?


This topic is 6063 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hi, all!

In the current solution I'm working on there are multiple buttons for users to select which scripts they'd like to run. The problem is, how do I prevent a second script from running while the first one is running?

I searched the archives here and found 2001 post where a virtual latch was used so whenever a script begins to run, it causes a control field to turn to "1"; when finished with the script it turns that field back to "0". (In the beginning of each script there's a command to check the control field; if it's "1", the second script is terminate using a halt command, if "0" it proceeds.)

I tried using the aforementioned script technique and it still doesn't quite work as I thought it would: the first script "hangs" after the second script bails out.

Suggestions for a tweak, or a different approach to this problem, would be greatly appreciated!

As always, TIAFYT.

Cheers,

Rich

Link to comment
Share on other sites

The situation is not quite clear. I believe that scripts are queued, so a second script wouldn't run until the first one has finished or paused - whether you do something about it or not.

I am also not sure what you mean by "the first script "hangs" after the second script bails out". If the second script executes a Halt Script[] step, I believe ALL scripts will terminate immediately.

Link to comment
Share on other sites

I agreed with Comment. Usually, if the second script continued, the problem would be not the function of Halt [], but Exit[] script. So, it will only exit the current script. However, if you have multi sub-script, it will be better to use Halt [] to break all script at once...

Link to comment
Share on other sites

(Sorry for the delay in responding to your notes, guys--I work two full-time jobs to make ends meet so sometimes I can't reply as quickly as I'd like to.)

When I said that the first script hangs, I meant that it just stops mid-execution and doesn't continue after it's suddenly interrupted by the offending script. What I'd like to happen is that if the first script is running, and a user accidentally clicks on another button, the second script would be automatically terminated and the first script would continue to run.

Maybe it's the command structure I'm using: the "halt" script is as follows:

Allow user abort [off]

If [Main::Script_Go_NoGo = "1"]

Halt Script

Else

Set Field [Main::Script_Go_NoGo = "1"]

End If

...and the last line of each script is:

Set Field [Main::Script_Go_NoGo = "0"]

So, in theory anyway, once the second script "sees" the flag is set to 1 then it should terminate...but then again, I probably goofed-up in the coding...again. *heavy sigh*

Link to comment
Share on other sites

WF7A, you can use Exit Script (exits current script) instead of Halt Script (halts all scripts). Exit script also has an option to return a Result; though I've found it is a bit unreliable unless I initiated it when I didn't want to pass a result.

Example:

If [ Get (LastError) ≠ 0 ]

Exit Script ( "Error" )

Else

Exit Script ("")

End If

Worked. If I left out the Exit Script (""), then sometimes "Error" would show up anyway in the calling script when there was no error. I watched it in Data Viewer. Anyone else see this, or, preferrably, not see this?

Link to comment
Share on other sites

This topic is 6063 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.