QuickOPC User's Guide and Reference
UnsubscribeEvents Method (_EasyAEClient)
Example 



OpcLabs.EasyOpcClassic Assembly > OpcLabs.EasyOpc.AlarmsAndEvents.ComTypes Namespace > _EasyAEClient Interface : UnsubscribeEvents Method
Event subscription handle as returned by the SubscribeEvents method
Unsubscribe from particular OPC event notifications.
Syntax
'Declaration
 
Sub UnsubscribeEvents( _
   ByVal handle As Integer _
) 
'Usage
 
Dim instance As _EasyAEClient
Dim handle As Integer
 
instance.UnsubscribeEvents(handle)
void UnsubscribeEvents( 
   int handle
)
void UnsubscribeEvents( 
   int handle
) 

Parameters

handle
Event subscription handle as returned by the SubscribeEvents method
Exceptions
ExceptionDescription

One of the arguments provided to a method is not valid.

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

The event subscription handle becomes invalid after this method is called.

 

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

.NET

.NET

COM

COM

// This example shows how to unsubscribe from specific event notifications.

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

namespace DocExamples.AlarmsAndEvents._EasyAEClient
{
    class UnsubscribeEvents
    {
        public static void Main1()
        {
            // Instantiate the client object.
            using (var client = new EasyAEClient())
            {
                var eventHandler = new EasyAENotificationEventHandler(client_Notification);
                client.Notification += eventHandler;

                Console.WriteLine("Subscribing...");
                int handle = client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000);

                Console.WriteLine("Waiting for 10 seconds...");
                Thread.Sleep(10 * 1000);

                Console.WriteLine("Unsubscribing...");
                client.UnsubscribeEvents(handle);

                Console.WriteLine("Waiting for 10 seconds...");
                Thread.Sleep(10 * 1000);
            }
        }

        // Notification event handler
        static void client_Notification(object sender, EasyAENotificationEventArgs e)
        {
            if (!e.Succeeded)
            {
                Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief);
                return;
            }
            if (!(e.EventData is null))
                Console.WriteLine(e.EventData.Message);
        }
    }
}
# This example shows how to unsubscribe from specific event notifications.

# 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 event handler
def notification(sender, e):
    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()

client.Notification += notification

print('Subscribing events...')
handle = IEasyAEClientExtension.SubscribeEvents(client, '', 'OPCLabs.KitEventServer.2', 1000)

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

print('Unsubscribing events...')
client.UnsubscribeEvents(handle)

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

client.Notification -= notification

print('Finished.')
' This example shows how to unsubscribe from specific event notifications.

Imports System.Threading
Imports OpcLabs.EasyOpc.AlarmsAndEvents
Imports OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel

Namespace AlarmsAndEvents._EasyAEClient

    Friend Class UnsubscribeEvents
        Public Shared Sub Main1()
            Using client = New EasyAEClient()
                Dim eventHandler = New EasyAENotificationEventHandler(AddressOf client_Notification)
                AddHandler client.Notification, eventHandler

                Console.WriteLine("Subscribing...")
                Dim handle As Integer = client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000)

                Console.WriteLine("Waiting for 10 seconds...")
                Thread.Sleep(10 * 1000)

                Console.WriteLine("Unsubscribing...")
                client.UnsubscribeEvents(handle)

                Console.WriteLine("Waiting for 10 seconds...")
                Thread.Sleep(10 * 1000)
            End Using
        End Sub

        ' Notification event handler
        Private Shared Sub client_Notification(ByVal sender As Object, ByVal e As EasyAENotificationEventArgs)
            If Not e.Succeeded Then
                Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief)
                Exit Sub
            End If
            If e.EventData IsNot Nothing Then
                Console.WriteLine(e.EventData.Message)
            End If
        End Sub
    End Class
End Namespace
# This example shows how to subscribe to events and display the event message with each notification. It also shows how to 
# unsubscribe afterwards.

#requires -Version 5.1
using namespace OpcLabs.EasyOpc.AlarmsAndEvents

# The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows .
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassicCore.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassic.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassicComponents.dll"

# Instantiate the client object.
$client = New-Object EasyAEClient

# Notification event handler
Register-ObjectEvent -InputObject $client -EventName Notification -Action { 
    if (-not $EventArgs.Succeeded) {
        Write-Host "*** Failure: $($EventArgs.ErrorMessageBrief)"
        #return
    }
    if ($EventArgs.EventData -ne $null) {
        Write-Host $EventArgs.EventData.Message
    }
}

Write-Host "Subscribing events..."
$handle = [IEasyAEClientExtension]::SubscribeEvents($client, "", "OPCLabs.KitEventServer.2", 1000)

Write-Host "Processing event notifications for 1 minute..."
$stopwatch =  [System.Diagnostics.Stopwatch]::StartNew() 
while ($stopwatch.Elapsed.TotalSeconds -lt 60) {    
    Start-Sleep -Seconds 1
}

Write-Host "Unsubscribing events..."
$client.UnsubscribeEvents($handle)

Write-Host "Finished."
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.

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.

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 unsubscribe from specific event notifications.

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..."
Dim SubscriptionParameters: Set SubscriptionParameters = CreateObject("OpcLabs.EasyOpc.AlarmsAndEvents.AESubscriptionParameters")
SubscriptionParameters.NotificationRate = 1000
Dim handle: handle = Client.SubscribeEvents(ServerDescriptor, SubscriptionParameters, True, Nothing)

WScript.Echo "Waiting for 10 seconds..."
WScript.Sleep 10*1000

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

WScript.Echo "Waiting for 10 seconds..."
WScript.Sleep 10*1000



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