Part 3: Stopping Multiple Loops Simultaneously
- Updated2025-02-17
- 6 minute(s) read
Part 3: Stopping Multiple Loops Simultaneously
Creating a safe shutdown procedure is a fundamental component of a real-time application. For example, if one part of your application controls a piece of equipment and another part of your application monitors for alarm conditions, you may want to stop the equipment when you encounter an alarm condition. In such a scenario, you can use a network-published shared variable to merge the stop and error functions of multiple loops into one.
Creating a Shared Variable
Network-published shared variables are ideal for broadcasting shutdown events because they can communicate to multiple VIs and because the read and write operations for network-published shared variables do not block each other.
Complete the following steps to configure a network-published shared variable:
- In the Project Explorer window, right-click the RT target and select New»Variable from the short-cut menu to display the Shared Variable Properties dialog box.
- Configure the variable by completing the following steps:
- In the Variable page of the Shared Variable Properties dialog box, enter Active? in the Name text box.
- Select Network-Published from the Variable Type pull-down menu.
- Select Boolean from the Data Type pull-down menu.
- In the RT FIFO page of the Shared Variable Properties dialog box, place a check mark in the Enable RT FIFO check box.
- Click OK.
- In the Project Explorer window, right-click the library that contains the Active? variable and select Save»Save from the short-cut menu.
- Name the library Variables.
- Click OK.
- Click and drag the Active? variable from the Project Explorer window onto the block diagram of Real-Time Main.vi and place it to the left of the RT FIFO Create function.
- Right-click the Active? variable and select Access Mode»Write from the short-cut menu.
- Wire a True constant to the Active? input of the Active? variable.
Initializing the Variable
When you launch your application, many parts of code load in parallel. This means that some parts of your application might load before other parts are ready. To prevent the application from stopping because of insufficient initialization time for the network-published shared variable, it is a best practice to check for proper initialization using a While Loop.
Complete the following steps to initialize the Active? variable:
- Add a While Loop between the Active? variable and the RT FIFO Create function.
- Create a copy of the Active? variable and place it within the While Loop.
- Right-click the Active? variable within the While Loop and select Access Mode»Read from the short-cut menu.
- Add an Unbundle by Name function within the While Loop.
- Wire the Unbundle by Name function input to the error out output of the Active? variable.
- Right-click the Unbundle by Name function and select Select Item»Code from the short-cut menu.
- Add a Not Equal? function within the While Loop.
- Wire the code output of the Unbundle by Name function to the x input of the Not Equal? function.
- Right-click the y input of the Not Equal? function and select Create»Constant from the short-cut menu.
- Enter -1950679034 as the constant. This is the error code that LabVIEW returns if the shared variable has no value.
- Wire the x!=y? output of the Not Equal? function to the stop condition of the While Loop.
- Add a Wait (ms) function within the While Loop.
- Right-click the milliseconds to wait input of the Wait (ms) function and select Create»Constant from the short-cut menu.
- Enter 10 as the constant to check whether the variable is
initialized every 10 milliseconds.

Creating the Shutdown Condition
A shutdown condition ensures that every process stops when there is a critical error in one part of the application or when the user stops the application.
Complete the following steps to create a safe shutdown condition using the shared variable:
- Wire the error out output of the Active? variable to the error in input of the Active? variable within the While Loop.
- Wire the error out output of the Active? variable within the While Loop to the error in input of the RT FIFO Create function.
- Delete the error wire between the RT FIFO Write function and the Right Data node of the Timed Loop.
- Delete the error wire between the Output node of the Timed Loop and the Merge Errors function.
- Copy the Active? variable and paste the copy within the Timed Loop and to the right of the RT FIFO Write function.
- Wire the error out output of the RT FIFO Write function to the error in input of the Active? variable within the Timed Loop.
- Right-click the Active? variable within the Timed Loop and select Access Mode»Read from the short-cut menu.
- Place a Not function to the right of the Active? variable within the Timed Loop.
- Wire the Active? output of the Active? variable to the x input of the Not function.
- Place an Or function to the right of the Not function.
- Wire the .not. x? output of the Not function to the x input of the Or function.
- Wire the error out output of the Active? variable within the Timed Loop to the y input of the Or function.
- Place a Case structure outside and to the right of the Timed Loop.
- Connect the error wire of the Active? variable within the Timed Loop to the Error input of the Right Data node of the Timed Loop.
- Wire the Error output of the Output node of the Timed Loop to the case selector of the Case structure.
- With Error selected in the selector label, place a copy of the Active? variable within the Case structure.
- Right-click the Active? variable and select Access Mode»Write from the short-cut menu.
- Wire a False constant to the Active? input of the Active? variable.
- Connect the error wire through the Case structure, but do not wire it to the
Active? variable. Note Make sure to complete this step again with No Error selected in the selector label.
- Connect the error wire leaving the Case structure to the error in input of the Merge Errors function.
- Delete the Stop button of the Loop Condition for the Timed Loop.
- Wire the x .or. y? output of the Or function to the Loop
Condition of the Timed Loop.

- Repeat steps 3 through 22 for the consumer loop to match the image. Note Make sure that the error wire from the RT FIFO Read function to the Active? variable goes through the Case structure.
- Save the VI.
Stopping the VI
At this point in the tutorial, the VI runs continuously because you cannot change the Active? variable to False from the front panel. To stop the VI, you can use the NI Distributed System Manager to change the value of the Active? variable. The NI Distributed System Manager is a useful tool for monitoring your network-published shared variables while your code is executing.
Complete the following steps to shut down the VI without using the Abort Execution button.
- Open the NI Distributed System Manager.
- Expand the Network Items folder in the Name column.
- Locate and expand your RT target root.
- Expand the Variables folder.
- Select the Active? variable.
- In the Auto View window, set New Value to False.
- Click Set.
- Close the Distributed System Manager.
Result
When you run the VI, the loops stop simultaneously if the Active? variable is False or if LabVIEW detects an error.