OPC Studio User's Guide and Reference
SubscribeEvents(IEasyAEClient,String,String,Int32,EasyAENotificationEventHandler) Method
Example 



OpcLabs.EasyOpcClassicCore Assembly > OpcLabs.EasyOpc.AlarmsAndEvents Namespace > IEasyAEClientExtension Class > SubscribeEvents Method : SubscribeEvents(IEasyAEClient,String,String,Int32,EasyAENotificationEventHandler) Method
The client object that will perform the operation.

This is typically the EasyAEClient object.

The value of this parameter cannot be null (Nothing in Visual Basic).

Name of the machine. Determines the computer on which the OPC server is located. It may be an empty string, in which case the OPC server is assumed to exist on the local computer or at the computer specified for it by DCOM configuration.

The value represents a UNC or DNS computer name. Any string can be passed to this parameter (i.e. will not cause System.ArgumentException), but not all values make sense and will work when an operation using them is attempted. IPv6 addresses are normally enclosed between '[' and ']'.

The value of this parameter cannot be null (Nothing in Visual Basic).

Contains ProgID of the OPC server.

The value of this parameter cannot be null (Nothing in Visual Basic).

The requested notification rate. The notification rate is in milliseconds and tells the server how often to send event notifications. This is a minimum time - do not send event notifications any faster that this UNLESS the buffer size is reached. A value of 0 for notification rate means that the server should send event notifications as soon as it gets them. This parameter is used to improve communications efficiency between client and server. This parameter is a recommendation from the client, and the server is allowed to ignore the parameter.
A callback method to be invoked for each OPC event.

The value of this parameter can be null (Nothing in Visual Basic).

Subscribe to OPC events. Specify machine name, server class, notification rate, and a callback method. The subscription is created active, and it will automatically perform a Refresh after each successful connection to the server. No event attributes will be returned with the notifications, and events will not be filtered.
Syntax
'Declaration
 
<ExtensionAttribute()>
Public Overloads Shared Function SubscribeEvents( _
   ByVal client As IEasyAEClient, _
   ByVal machineName As String, _
   ByVal serverClass As String, _
   ByVal notificationRate As Integer, _
   ByVal callback As EasyAENotificationEventHandler _
) As Integer
'Usage
 
Dim client As IEasyAEClient
Dim machineName As String
Dim serverClass As String
Dim notificationRate As Integer
Dim callback As EasyAENotificationEventHandler
Dim value As Integer
 
value = IEasyAEClientExtension.SubscribeEvents(client, machineName, serverClass, notificationRate, callback)

Parameters

client
The client object that will perform the operation.

This is typically the EasyAEClient object.

The value of this parameter cannot be null (Nothing in Visual Basic).

machineName
Name of the machine. Determines the computer on which the OPC server is located. It may be an empty string, in which case the OPC server is assumed to exist on the local computer or at the computer specified for it by DCOM configuration.

The value represents a UNC or DNS computer name. Any string can be passed to this parameter (i.e. will not cause System.ArgumentException), but not all values make sense and will work when an operation using them is attempted. IPv6 addresses are normally enclosed between '[' and ']'.

The value of this parameter cannot be null (Nothing in Visual Basic).

serverClass
Contains ProgID of the OPC server.

The value of this parameter cannot be null (Nothing in Visual Basic).

notificationRate
The requested notification rate. The notification rate is in milliseconds and tells the server how often to send event notifications. This is a minimum time - do not send event notifications any faster that this UNLESS the buffer size is reached. A value of 0 for notification rate means that the server should send event notifications as soon as it gets them. This parameter is used to improve communications efficiency between client and server. This parameter is a recommendation from the client, and the server is allowed to ignore the parameter.
callback
A callback method to be invoked for each OPC event.

The value of this parameter can be null (Nothing in Visual Basic).

Return Value

The method returns an integer handle that uniquely identifies the event subscription.
Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

The value of an argument is outside the allowable range of values as defined by the invoked method.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

Remarks
If callback is a null reference, only the Notification events will be generated, but no callback method will be invoked.

This is an extension method (info: C#, VB.NET). In languages that have support for extensions methods (such as C# and VB.NET), you can use the extension method as if it were a regular method on the object that is its first parameter. In other languages (such as with Python.NET), you will call the extension as a static method, and pass it the object on which it acts as its first parameter.

 

This method operates (at least in part) asynchronously, with respect to the caller. The actual execution of the operation may be delayed, and the outcome of the operation (if any) is provided to the calling code using an event notification, callback, or other means explained in the text. In a properly written program, this method does not throw any exceptions. You should therefore not put try/catch statements or similar constructs around calls to this method. The only exceptions thrown by this method are for usage errors, i.e. when your code violates the usage contract of the method, such as passing in invalid arguments or calling the method when the state of the object does not allow it. Any operation-related errors (i.e. errors that depend on external conditions that your code cannot reliably check) are indicated by the means the operation returns its outcome (if any), which is described in the text. For more information, see Do not catch any exceptions with asynchronous or multiple-operation methods.
Example
// This example shows how to subscribe to events and display the event message with each notification, using a callback method
// specified using lambda expression.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using System.Diagnostics;
using System.Threading;
using OpcLabs.EasyOpc.AlarmsAndEvents;

namespace DocExamples.AlarmsAndEvents._EasyAEClient
{
    partial class SubscribeEvents
    {
        public static void CallbackLambda()
        {
            // Instantiate the client object.
            var client = new EasyAEClient();

            Console.WriteLine("Subscribing...");
            // The callback is a lambda expression the displays the event message
            client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000,
                (sender, eventArgs) =>
                {
                    Debug.Assert(eventArgs != null);
                    if (!eventArgs.Succeeded)
                    {
                        Console.WriteLine("*** Failure: {0}", eventArgs.ErrorMessageBrief);
                        return;
                    }
                    if (!(eventArgs.EventData is null))
                        Console.WriteLine(eventArgs.EventData.Message);
                });

            Console.WriteLine("Processing event notifications for 20 seconds...");
            Thread.Sleep(20 * 1000);

            Console.WriteLine("Unsubscribing...");
            client.UnsubscribeAllEvents();

            Console.WriteLine("Waiting for 2 seconds...");
            Thread.Sleep(2 * 1000);
        }
    }
}
' This example shows how to subscribe to events and display the event message with each notification, using a callback method
' specified using lambda expression.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasyOpc.AlarmsAndEvents

Namespace AlarmsAndEvents._EasyAEClient
    Partial Friend Class SubscribeEvents
        Public Shared Sub CallbackLambda()
            ' Instantiate the client object
            Dim client = New EasyAEClient()

            Console.WriteLine("Subscribing...")
            ' The callback is a lambda expression the displays the event message
            client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000,
                    Sub(sender, eventArgs)
                        Debug.Assert(eventArgs IsNot Nothing)
                        If Not eventArgs.Succeeded Then
                            Console.WriteLine("*** Failure: {0}", eventArgs.ErrorMessageBrief)
                            Exit Sub
                        End If
                        If eventArgs.EventData IsNot Nothing Then
                            Console.WriteLine(eventArgs.EventData.Message)
                        End If
                    End Sub)

            Console.WriteLine("Processing event notifications for 20 seconds...")
            Threading.Thread.Sleep(20 * 1000)

            Console.WriteLine("Unsubscribing...")
            client.UnsubscribeAllEvents()

            Console.WriteLine("Waiting for 2 seconds...")
            Threading.Thread.Sleep(2 * 1000)
        End Sub
    End Class
End Namespace
# This example shows how to subscribe to events and display the event message with each notification, using a regular
# callback method.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time

# Import .NET namespaces.
from OpcLabs.EasyOpc import *
from OpcLabs.EasyOpc.AlarmsAndEvents import *


# Notification callback.
def callback(sender, e):
    assert e is not None
    if not e.Succeeded:
        print('*** Failure: ', e.ErrorMessageBrief, sep='')
        return
    else:
        if e.EventData is not None:
            print(e.EventData.Message)


# Instantiate the client object
client = EasyAEClient()

print('Subscribing events...')
# The callback is a regular method that  displays the event message.
handle = IEasyAEClientExtension.SubscribeEvents(client,
                                                '', 'OPCLabs.KitEventServer.2', 1000,
                                                EasyAENotificationEventHandler(callback))

print('Processing event notifications for 20 seconds...')
time.sleep(20)

print('Unsubscribing events...')
client.UnsubscribeAllEvents()

print('Waiting for 2 seconds...')
time.sleep(2)

print('Finished.')
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also