04-21-2011 02:44 PM
I have multiple loops running in parallel and want them all to stop looping based on whether either of the two conditions are true:
Right now I'm doing this by using a master loop that checks if any of the loops have an error, or if the stop button is pressed.
The problem I'm having is that I'm making references between the loops using T/F status lights. These light up if any of the loops stop. This means that at the end of a test all of the lights are lit up, and before the program is run again every light must be unchecked. This isn't user friendly and as my program grows I don't want to have to uncheck 20 lights before each test.
Is there a way to make the lights reset at the beginning of each test, or is there a better way to reference the status of each loop?
Solved! Go to Solution.
04-21-2011 03:02 PM
Create a functional global VI called AllStop. Write to it in any loop where an error condition is True or when the Stop button is pressed. Read it in every loop and use its output to stop the loops. Initialize it to false when the program starts.
Lynn
04-21-2011 03:05 PM
Create a functional global VI called AllStop. Write a True to it in any loop where the error condition requires stopping or when the Stop button is pressed. Read it in each loop and connect its output to the stop terminal in the loop. Initialize it to False when the program starts.
Lynn
04-21-2011 03:20 PM - edited 04-21-2011 03:25 PM
An alternative is to use a Notifier.
Set up a notifier with the data type being a boolean.
Wire the stop terminal of both loops to the output of a Wait For Notification (dont forget to set a timeout).
If an error occurs, or stop is pressed then send a notification.
Clicking Stop will stop both loops. Or an error in either loop will stop both.
This isnt any more complicated than an FG I think.
04-21-2011 03:35 PM - edited 04-21-2011 03:38 PM
Or, to keep it your way now, you could use a 'Reinitialize To Default Values' invoke node at the beginning of your program.
This way, everytime you restart your program, the lights will be off (assuming that their default value is off).
02-14-2012 09:11 AM
@ Lynn. How to create a functional global as you mentioned above? I am also trying to stop 3 parallel loops and want the fastest and most efficient option. Also, When creating an exe, (application) will using functional global work well?
02-14-2012 09:38 AM
VeeJay,
From the LV help page titled "Suggestions for Using Execution Systems and Prioritites":
Another way to avoid race conditions associated with global variables is to use functional global variables. Functional global variables are VIs that use loops with uninitialized shift registers to hold global data. A functional global variable usually has an action input parameter that specifies which task the VI performs. The VI uses an uninitialized shift register in a While Loop to hold the result of the operation. The following illustration shows a functional global variable that implements a simple count global variable. The actions in this example are initialize, read, increment, and decrement.

Every time you call the VI, the block diagram in the loop runs exactly once. Depending on the action parameter, the case inside the loop initializes, does not change, incrementally increases, or incrementally decreases the value of the shift register.
Although you can use functional global variables to implement simple global variables, as shown in the previous example, they are especially useful when implementing more complex data structures, such as a stack or a queue buffer. You also can use functional global variables to protect access to global resources, such as files, instruments, and data acquisition devices, that you cannot represent with a global variable, since calls to the functional global variable execute sequentially unless you mark the VI as reentrant.
You can solve most synchronization problems with functional global variables, because the functional global VI ensures that only one caller at a time changes the data it contains. One disadvantage of functional global variables is that when you want to change the way you modify the resource they hold, you must change the functional global VI block diagram and add a new action. In some applications, where the use of global resources changes frequently, these changes might be inconvenient. In such cases, design the application to use a semaphore to protect access to the global resource.
Also, look at Ben's Nugget on Action Engines. An Action Engine is a super functional globaland is more than you need for stopping your loops.
Lynn
02-14-2012 10:33 AM
THanks Lynn! THat was helpful. I am currently using Notifiers to stop loops. On highlight execution, I find that all loops have stopped, but the "run" button is still ON, i.e. program is still running. This is so weird, because I am not able to trace which loop is still active because all loops have ended. Stumped!! 😞
02-14-2012 10:40 AM
Sometimes highlight execution can be deceptive. Depending on your version of LV and when you turned on highlight execution, the loop which was running at that instant may not show the green arrow but still is running.
Do you have an event structure? Might it be waiting for an event before the loop containing it completes?
If those clues do not help, then we will need to see your code.
Notifiers are a good way to stop loops.
Lynn