Processing zenon variable information in a VSTA thread using a thread safe queue
There is often the need to process zenon data (e.g. variable values, etc.) in VSTA without influencing time critical functions of the zenon Runtime itself. With VSTA/.NET and the usage of multithreading you have new possibilities which you did not have with VBA before (where the Runtime was blocked when executing time consuming VBA function).
Nevertheless when using multithreading you still have to keep in mind that objects from the zenon object model must be only accessed from one thread at a given time.
The following example will show how to gather zenon variable information in a zenon event, write this information into a custom object and how to put these objects in a thread safe queue. The queue itself is then be read out by the use of a worker thread.
In the class ThisProject you need to create an Online Container and a queue for the custom variable items.
zenOn.IOnlineVariable MyOnlineContainer = null;
//Queue for custom variable objects
ZenonQueue<Variable> Queue = null;
In ThisProject_Startup, the event handlers for Project Active/Inactive are created.
privatevoid ThisProject_Startup(object sender, EventArgs e)
//Create Eventhandlers for ThisProject Active/Inactive
this.Active += new zenOn.ActiveEventHandler(ThisProject_Active);
this.Inactive += new zenOn.InactiveEventHandler(ThisProject_Inactive);
In ThisProject_Active, the Online Container and the queue are initialized.
//Create a new queue of custom variable objects
Queue = newZenonQueue<Variable>();
//Create Online Container, add two variables, create event handler
MyOnlineContainer.VariableChange += new
The uninitialization is done in the ThisProject_Inactive/ThisProject_Shutdown.
//Undefine,defererence Online Container, release event handler
MyOnlineContainer.VariableChange -= new
MyOnlineContainer = null;
//Dereference the queue
Queue = null;
privatevoid ThisProject_Shutdown(object sender, EventArgs e)
//Release event handlers
this.Active -= new zenOn.ActiveEventHandler(ThisProject_Active);
this.Inactive -= new zenOn.InactiveEventHandler(ThisProject_Inactive);
In the Variable Change Event of the Online Container, a custom variable object containing information of a zenon variable is created and put in the queue.
void MyOnlineContainer_VariableChange(zenOn.IVariable obVar)
//Create a new custom variable object with the information (name and
value) of a (numerical) zenon variable
The items of the queue are taken out from the queue and written to the Immediate Window of the VSTA Editor using a zenon VSTA Macro and a thread.
//Create a thread which dequeues all items from the queue and writes the
name and value into the Immediate Window
Thread consumerThread = newThread(() =>
while (Queue.GetCount() > 0)
Variable var = Queue.Dequeue();
System.Diagnostics.Debug.Print(var._name + “, “ +
The class Variable.cs is the class for our custom variable object.
The class ZenonQueue.cs is the class for our thread safe queue containing the custom variable objects.
For more information please have a look on the source code.