OPC Studio User's Guide and Reference
SubscribeEvents Method (_EasyAEClient)
Example 



OpcLabs.EasyOpcClassic Assembly > OpcLabs.EasyOpc.AlarmsAndEvents.ComTypes Namespace > _EasyAEClient Interface : SubscribeEvents Method
The OPC server involved in the operation.
Subscription parameters to be used when the subscription is created
When set, the subscription will automatically perform a Refresh after each successful connection to the server (either the initial connection, or any automatic reconnection after the connection is lost).
The state object (can be any value supplied by your code); available in event notifications.
Subscribe to OPC events. Specify server descriptor and subscription parameters objects, and "refresh when active" flag.
Syntax
'Declaration
 
Function SubscribeEvents( _
   ByVal serverDescriptor As Object, _
   ByVal subscriptionParameters As Object, _
   ByVal refreshWhenActive As Boolean, _
   ByVal state As Object _
) As Integer
'Usage
 
Dim instance As _EasyAEClient
Dim serverDescriptor As Object
Dim subscriptionParameters As Object
Dim refreshWhenActive As Boolean
Dim state As Object
Dim value As Integer
 
value = instance.SubscribeEvents(serverDescriptor, subscriptionParameters, refreshWhenActive, state)

Parameters

serverDescriptor
The OPC server involved in the operation.
subscriptionParameters
Subscription parameters to be used when the subscription is created
refreshWhenActive
When set, the subscription will automatically perform a Refresh after each successful connection to the server (either the initial connection, or any automatic reconnection after the connection is lost).
state
The state object (can be any value supplied by your code); available in event notifications.

Return Value

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

COM

// This example shows how to subscribe to events and display the event message with each notification. It also shows how to
// unsubscribe afterwards.
//
// Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

type
  TClientEventHandlers = class
    // Notification event handler
    procedure OnNotification(
      ASender: TObject;
      sender: OleVariant;
      const eventArgs: _EasyAENotificationEventArgs);
  end;

procedure TClientEventHandlers.OnNotification(
  ASender: TObject;
  sender: OleVariant;
  const eventArgs: _EasyAENotificationEventArgs);
begin
  if not eventArgs.Succeeded then
    WriteLn(Format('*** Failure: %s', [eventArgs.ErrorMessageBrief]));
  if eventArgs.EventData <> nil then
      WriteLn(eventArgs.EventData.Message);
end;

class procedure SubscribeEvents.Main;
var
  Client: TEasyAEClient;
  ClientEventHandlers: TClientEventHandlers;
  Handle: Integer;
  ServerDescriptor: _ServerDescriptor;
  State: OleVariant;
  SubscriptionParameters: _AESubscriptionParameters;
begin
  ServerDescriptor := CoServerDescriptor.Create;
  ServerDescriptor.ServerClass := 'OPCLabs.KitEventServer.2';

  // Instantiate the client object and hook events
  Client := TEasyAEClient.Create(nil);
  ClientEventHandlers := TClientEventHandlers.Create;
  Client.OnNotification := ClientEventHandlers.OnNotification;

  WriteLn('Subscribing events...');
  SubscriptionParameters := CoAESubscriptionParameters.Create;
  SubscriptionParameters.NotificationRate := 1000;
  Handle := Client.SubscribeEvents(ServerDescriptor, SubscriptionParameters, true, State);

  WriteLn('Processing event notifications for 1 minute...');
  PumpSleep(60*1000);

  WriteLn('Unsubscribing events...');
  Client.UnsubscribeEvents(Handle);

  WriteLn('Finished.');
  FreeAndNil(Client);
  FreeAndNil(ClientEventHandlers);
end;
// This example shows how to subscribe to events and display the event message with each notification. It also shows how to
// unsubscribe afterwards.
//
// Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

class DEasyEAClientEvents {
    function Notification($Sender, $E)
    {
        if (!($E->Succeeded))
        {
            printf("*** Failure: %s\n", $E->ErrorMessageBrief);
            Exit();
        }

        if (!is_null($E->EventData))
        {
            print $E->EventData->Message;
            print "\n";
        }
    }
}

$ServerDescriptor = new COM("OpcLabs.EasyOpc.ServerDescriptor");
$ServerDescriptor->ServerClass = "OPCLabs.KitEventServer.2";

$Client = new COM("OpcLabs.EasyOpc.AlarmsAndEvents.EasyAEClient");
$Events = new DEasyEAClientEvents();
com_event_sink($Client, $Events, "DEasyEAClientEvents");

print "Subscribing events...\n";
$SubscriptionParameters = new COM("OpcLabs.EasyOpc.AlarmsAndEvents.AESubscriptionParameters");
$SubscriptionParameters->NotificationRate = 1000;
$handle = $Client->SubscribeEvents($ServerDescriptor, $SubscriptionParameters, TRUE, NULL);

print "Processing event notifications for 1 minute...\n";
$startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 60);

print "Unsubscribing events...\n";
$Client->UnsubscribeEvents($handle);

print "Finished.\n";
REM This example shows how to subscribe to events and display the event message with each notification. It also shows how to
REM unsubscribe afterwards.
REM
REM Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Private Sub SubscribeEvents_Main_Command_Click()
    OutputText = ""
    
    Dim serverDescriptor As New serverDescriptor
    serverDescriptor.ServerClass = "OPCLabs.KitEventServer.2"
    
    ' Instantiate the client object and hook events
    Set Client1 = New EasyAEClient
    
    OutputText = OutputText & "Subscribing..." & vbCrLf
    Dim subscriptionParameters As New AESubscriptionParameters
    subscriptionParameters.notificationRate = 1000
    Dim handle
    Dim state
    handle = Client1.SubscribeEvents(serverDescriptor, subscriptionParameters, True, state)

    OutputText = OutputText & "Processing event notifications for 1 minute..." & vbCrLf
    Pause 60000

    OutputText = OutputText & "Unsubscribing events..." & vbCrLf
    Client1.UnsubscribeEvents handle

    OutputText = OutputText & "Waiting for 5 seconds..." & vbCrLf
    Pause 5000

    OutputText = OutputText & "Finished." & vbCrLf

    Set Client1 = Nothing
End Sub

Private Sub Client1_OnNotification(ByVal sender As Variant, ByVal eventArgs As EasyAENotificationEventArgs)
    If Not eventArgs.Succeeded Then
        OutputText = OutputText & eventArgs.ErrorMessageBrief & vbCrLf
        Exit Sub
    End If
    If Not eventArgs.EventData Is Nothing Then
        OutputText = OutputText & eventArgs.EventData.Message & vbCrLf
    End If
End Sub
Rem This example shows how to subscribe to events and display the event message with each notification. It also shows how to
Rem unsubscribe afterwards.
Rem
Rem Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Option Explicit

Dim ServerDescriptor: Set ServerDescriptor = CreateObject("OpcLabs.EasyOpc.ServerDescriptor")
ServerDescriptor.ServerClass = "OPCLabs.KitEventServer.2"

Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.AlarmsAndEvents.EasyAEClient")
WScript.ConnectObject Client, "Client_"

WScript.Echo "Subscribing events..."
Dim SubscriptionParameters: Set SubscriptionParameters = CreateObject("OpcLabs.EasyOpc.AlarmsAndEvents.AESubscriptionParameters")
SubscriptionParameters.NotificationRate = 1000
Dim handle: handle = Client.SubscribeEvents(ServerDescriptor, SubscriptionParameters, True, Nothing)

WScript.Echo "Processing event notifications for 1 minute..."
WScript.Sleep 60*1000

WScript.Echo "Unsubscribing events..."
Client.UnsubscribeEvents handle

WScript.Echo "Finished."



Rem Notification event handler
Sub Client_Notification(Sender, e)
    If Not e.Succeeded Then
        WScript.Echo "*** Failure: " & e.ErrorMessageBrief
        Exit Sub
    End If

    If Not e.EventData Is Nothing Then WScript.Echo e.EventData.Message
End Sub
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