Event Example
- Last UpdatedSep 18, 2024
- PI System
- AF SDK 2024
- Developer
The following simple example shows how to receive events from the AF SDK. In Visual Basic .NET, registering to receive events can be done through the use of the WithEvents statement at the top of the file, then by implementing the specific event handler methods. More flexibility is given in naming the event handlers through the use of the Handles clause of the event handler method definition. In C#, each event must have a new handler created, which is registered and unregistered using the += and -= statements, respectively.
Variable Declarations:
1// Declare Variables 2static System.Timers.Timer refreshTimer = new System.Timers.Timer(10 * 1000); // every 10 seconds 3static AFDatabase myDB = null; 4static int myEventCounter = 0;
1' Declare Variables 2Private Shared WithEvents refreshTimer As New System.Timers.Timer(10 * 1000) ' every 10 seconds 3Private Shared WithEvents myDB As AFDatabase 4Private Shared WithEvents myElementTemplates As AFElementTemplates 5Shared myEventCounter As Integer = 0
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
Code Body:
1// Get the Default Database 2PISystems myPISystems = new PISystems(); 3myDB = myPISystems.DefaultPISystem.Databases.DefaultDatabase; 4if (myDB == null) 5 throw new InvalidOperationException("There is no default database."); 6 7// Add handlers to object 8System.Timers.ElapsedEventHandler elapsedEH = new System.Timers.ElapsedEventHandler(OnElapsed); 9refreshTimer.Elapsed += elapsedEH; 10 11EventHandler<AFChangedEventArgs> changedEH = new EventHandler<AFChangedEventArgs>(OnChanged); 12myDB.Changed += changedEH; 13 14// Start the refresh timer which allows this application to periodically 15// query for changes made by other applications 16refreshTimer.Start(); 17 18// Make a change to a collection which generates a change event 19AFElementTemplates myElementTemplates = myDB.ElementTemplates; 20lock (myDB) 21{ 22 if (myDB.ElementTemplates["myElementTemplate"] == null) 23 { 24 Console.WriteLine("Adding an element template"); 25 AFElementTemplate myElementTemplate = myElementTemplates.Add("myElementTemplate"); 26 } 27 else 28 { 29 Console.WriteLine("Deleting an element template"); 30 myDB.ElementTemplates.Remove("myElementTemplate"); 31 } 32 myDB.CheckIn(); 33} 34 35// Remove handlers from object 36myDB.Changed -= changedEH; 37refreshTimer.Elapsed -= elapsedEH;
1Dim myElementTemplate As AFElementTemplate 2 3' Get the Default Database 4Dim myPISystems As New PISystems 5myDB = myPISystems.DefaultPISystem.Databases.DefaultDatabase 6If myDB Is Nothing Then 7 Throw New InvalidOperationException("There is no default database.") 8End If 9 10' Start the refresh timer which allows this application to periodically 11' query for changes made by other applications 12refreshTimer.Start() 13 14' Make a change to a collection 15SyncLock myDB 16 myElementTemplates = myDB.ElementTemplates 17 If (myDB.ElementTemplates("myElementTemplate") Is Nothing) Then 18 Console.WriteLine("Adding an element template") 19 myElementTemplate = myElementTemplates.Add("myElementTemplate") 20 Else 21 Console.WriteLine("Deleting an element template") 22 myDB.ElementTemplates.Remove("myElementTemplate") 23 End If 24 25 myDB.CheckIn() 26End SyncLock
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
Event Handlers:
1internal static void OnChanged(object sender, AFChangedEventArgs e) 2{ 3 myEventCounter++; 4 Console.WriteLine("Object Changed Event Raised"); 5} 6 7internal static void OnElapsed(object sender, System.Timers.ElapsedEventArgs e) 8{ 9 // Refreshing Database will cause any external changes to be seen which will 10 // result in the triggering of the OnChanged event handler 11 lock (myDB) 12 { 13 myDB.Refresh(); 14 } 15 refreshTimer.Start(); 16}
1Private Shared Sub myDB_Changed(ByVal sender As Object, ByVal e As AFChangedEventArgs) Handles myDB.Changed 2 Application.DoEvents() 3 myEventCounter += 1 4 Console.WriteLine("Object Changed Event Raised") 5End Sub 6 7Private Shared Sub refreshTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles refreshTimer.Elapsed 8 ' Refreshing Database will cause any external changes to be seen which will 9 ' result in the triggering of the myDB_Changed event handler 10 SyncLock myDB 11 myDB.Refresh() 12 End SyncLock 13 refreshTimer.Start() 14End Sub
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.