Return Data From A .NET Event Callback VI in LabVIEW

Updated Oct 26, 2023



  • LabVIEW

Programming Language

  • C# .NET
  • Visual Basic .NET

When registering and handling .NET and ActiveX events in LabVIEW, an Event Callback VI must be created using the Register Event Callback function and used to handle the event properly. This document explains how to return data obtained using an Event Callback VI to the main VI which is calling it so that that data can be used in other locations in your program.

Once you have defined the event you're registering, follow the below steps to create your Event Callback VI and pass data from it to your main VI:
  1. Create a reference to a data structure, such as a queue, notifier, or user event, for the data you need to obtain from your callback VI, and pass this data structure reference to the User Parameter input of the Register Event Callback function you have defined for the event you're monitoring. In the example explained in this document, we will pass the Register Event Callback function a queue of string data.
Creation of queue that is passed to User Parameter input for use in our .NET event callback.
  1. Right-click the VI Ref input on the Register Event Callback function, and select Create Callback VI. LabVIEW will automatically create a callback VI based on the event you selected and data wired to the user parameter input, which in this case is the queue we will use to pass data back to the calling VI.
Right click menu option which allows you to create a callback VI based on your chosen event and user parameter.
  1. Open your created callback VI, and add code to enqueue the value you want to pass to the calling VI in your data structure. In this example, we're taking the full path to a created file's location and adding it to the queue.
Functionality in callback function which adds element to queue that can then be read out in calling VI.
  1. Now that this value is contained in a queue, we can dequeue it in our main VI and use it in other parts of the code/display it to the user. If you'd like to ensure that you have an element in the queue before attempting to dequeue, use the Get Queue Status function to get the number of elements in the queue, and only dequeue an element if one is present.
Demonstration of dequeuing of added element from callback VI, and checking performed beforehand to ensure that queue has an element in it.
  1. After your code finishes executing, be sure to close the reference to your data structure when cleaning up the rest of your references.
Cleanup of .NET and queue references.