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



View with Navigation Tools
OpcLabs.EasyOpcClassic Assembly > OpcLabs.EasyOpc.AlarmsAndEvents.ComTypes Namespace > _EasyAEClient Interface : SubscribeEvents Method
The OPC server involved in the operation.

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

Subscription parameters to be used when the subscription is created

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

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.

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

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.

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

subscriptionParameters
Subscription parameters to be used when the subscription is created

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

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.

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.
Remarks

This member or type is for use from COM. It is not meant to be used from .NET or Python. Refer to the corresponding .NET member or type instead, if you are developing in .NET or Python.

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 .
// OPC client and subscriber examples in Object Pascal (Delphi) on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-OP .
// 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.

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;
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