Processing zenon variable information in a VSTA thread using a thread safe queue

Introduction

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.

Class ThisProject

In the class ThisProject you need to create an Online Container and a queue for the custom variable items.

//Online Container

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.

void ThisProject_Active()

{

     //Create a new queue of custom variable objects

     Queue = newZenonQueue<Variable>();

 

     //Create Online Container, add two variables, create event handler

     MyOnlineContainer =    
     t
his.OnlineVariables().CreateOnlineVariables(“OnlineVar”);
     MyOnlineContainer.Add(
“var1”);

     MyOnlineContainer.Add(“var2”);

     MyOnlineContainer.Define();

     MyOnlineContainer.VariableChange += new
     zenOn.
VariableChangeEventHandler(MyOnlineContainer_VariableChange);

}

The uninitialization is done in the ThisProject_Inactive/ThisProject_Shutdown.

 

void ThisProject_Inactive()

{    

     //Undefine,defererence Online Container, release event handler

     MyOnlineContainer.Undefine();

     MyOnlineContainer.VariableChange -= new
     zenOn.
VariableChangeEventHandler(MyOnlineContainer_VariableChange);

     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                 

     Queue.Enqueue(newVariable(obVar.Name, (double)obVar.get_Value(0)));

}

 

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.

publicvoid Macro_DeQueue()

{

     //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 + “, “ +    
               var._value.ToString());

          }

     });

     consumerThread.Start();

}

Helper Classes

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.

Download zip file!

Tags:

Leave a Reply