Example Code

Sharing .NET Objects Between a TestStand Execution and a TestStand User Interface

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • Teststand

Code and Documents

Attachment

Overview

 The following Example Program demonstrates how to share/pass a .NET Object between a TestStand execution and a custom user interface written in .NET.

 

Description

 

.NET Objects created in a TestStand execution cannot be directly accessed from a TestStand user interface. This is due to the fact that objects created in the sequence are in a separate Application Domain than the User Interface. .NET objects exist in a particular Application Domain, and an object is only accessible within the Application Domain in which it is created. Because TestStand Creates a new application domain for executing sequence files, the UI cannot directly access the object.

 

For more information on Application Domains, refer to the article below:

MSDN: Application Domains Overview

 

To address this limitation, you can access the object reference through the TestStand API to access the object in the TestStand Execution application domain rather than the UI application domain. To use this approach:

  1. Create an instance of a .NET object in TestStand and store the reference in a variable, such as a Local.
  2. Call a method of the object from the sequence.
  3. Send a UIMessage from the sequence to the Operator Interface.
  4. Implement a UI message handler in the UI which receives this message, gets the reference to the object, casts the object to the correct type, and calls a method of the object.

When using this approach, consider:

  • Set the lease time on the object by overriding InitializeLifetimeService() in the object so that it doesn't timeout for cross-appdomain access.
  • For an object to be used in this manner, it must either be derived from MarshalByRefObject or be serializable. (If it is serializable, then copies are made rather than accessing a reference to the original.)
  • Any objects you pass in or out of the members of this marshaled object need to be basic types, derived from MarshalByRefObject themselves, or serializable.

An alternative solution is to use .NET Remoting. For more information, see Sharing .NET Objects Between TestStand and LabVIEW using .NET Remoting.

 

Hardware and Software Requirements

TestStand 2017 or Compatible

Visual Studio 2013 or Compatible

 

Steps to Implement or Execute Code

  1. Extract the attached archive. 
  2. Open the UI, located in \UI\bin\x86\Release.
  3. In the UI, run the sequence.
  4. Observe that the method is called twice; once directly using a .NET step, then a second time from the UI, in response to a UI message.

 

 
Al B.
Staff Software Engineer - TestStand
CTA/CLD

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors