How Do I Asynchronously Start and Stop a VI in Teststand?

Updated Sep 23, 2019

Reported In

Software

  • TestStand
  • LabVIEW

Issue Details

When I use an Action step to call a VI with a while loop in it, TestStand will hang until that VI completes its execution. I want to start and run my VI in the background in TestStand and then programmatically stop it at a later point in my sequence. How can I do this?

Solution

You can accomplish this by using the Run VI Asynchronously step type in TestStand. This will run the VI in a new thread, allowing your TestStand sequence to continue past that step. To call a VI asynchronously, follow the steps below.

First, you must prepare your VI to be started and stopped by TestStand in LabVIEW.
  1. On your VI's Front Panel, place an Automation Refnum control from Controls Palette»Refnum»Automation Refnum.
  2. Next, right-click on the Automation Refnum and choose Select ActiveX Class»Browse...
  3. In the Select Object From Type Library window, find and select NI TestStand <year> API Version x.0 from the Type Library drop-down menu.
  4. In the Objects window, select SequenceContext. Select OK. The Automation Refnum should now read TS.SequenceContext.
  5. Switch to the Block Diagram. Create a property reference to the parameters in TestStand by right-clicking on the Sequence Context Refnum control and selecting Create»Property for TS.SequenceContext Class»Parameters.
  6. Right-click on the Parameters node in the SequenceContext Property Node, and select Create»Method for TS.PropertyObject Class»GetValBoolean.
  7. Place the SequenceContext Property Node and the PropertyObject Invoke Node inside the loop you will be dynamically stopping from TestStand to stop the VI.
  8. Wire the Sequence Context Refnum into the SequenceContext Property Node, and wire the reference from Parameters into the PropertyObject Invoke Node.
  9. Specify the parameter name and wire the Boolean value from GetValBoolean to the conditional terminal of the loop. 
  10. Save the VI.
        Next, you need to configure TestStand to call your modified VI asynchronously.
      1. Add a Run VI Asynchronously step into your sequence. This step can be found in the Insertion Palette in the LabVIEW Utility folder.
      2. In the Step Settings pane, leave the Hostname blank so that TestStand will default to the local host, and click on the LabVIEW icon.
      3. In the Edit LabVIEW VI Call window, enter the path of the VI you want to run, either manually or using the file explorer button.
      4. After the VI has been properly loaded, un-check the Default box for the TS.SequenceContext parameter and set the Value to ThisContext. Press OK.
      5. In the Variables pane, add a new Boolean parameter under the Parameters section named the same as the lookupString value you defined in LabVIEW (see above), and set the default value to False.
      6. In the Run VI Asynchronously Step Settings pane, select the Context to Pass as This Context for VI Arguments as Use Context of Calling Thread.
      7. To stop the VI, add a Statement step, and in the Expression box, enter Parameters.Stop = True.
      This method causes TestStand to execute the VI in a new thread, and uses a Parameter (named Stop in the example above) which your VI will access and read through the Sequence Context passed into it from TestStand. 

      Additionally, when running asynchronous code in general, you can use the termination monitor  to ensure that your VI stops if the TestStand execution is terminated or aborted by the user.
       

      Additional Information

      Due to multi-threading and the parallel nature of this method, you must take care to prevent or mitigate race conditions. Passing data to and from asynchronous VIs using this method is discouraged. This method should only be used to start and stop VIs that have no other data dependencies. Data transfer between asynchronously called VIs should be handled through Queues, Notifiers, or UI Messages in the related links.