Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA Enterprise SCADA PubSub API Reference

Subscribe to multiple topics and process their callbacks

Subscribe to multiple topics and process their callbacks

This scenario guides you through the steps required to subscribe to multiple topics and process their callbacks.

To identify the code associated with each step, see the code comments in the Example code sections.

To subscribe to multiple topics and process their callbacks

  1. Open the Program.cs file from the created project.
  2. Add the following private global variables that will be processed in the callback:
    • AutoResetEvent waitHandle = new AutoResetEvent(false);
    • List<String> topicList = new List<String>();
  3. Create the callback method to process the subscriptions by performing the following:
    1. Process the message if there are no errors.
    2. Lock the shared resource to make it thread safe.
    3. Set the state of the event to signal when all of the subscriptions have been processed.
  4. Open a PubSub connection.
  5. Confirm there is a connection with the PubSub system.
  6. Initialize the global AutoResetEvent waitHandle variable.
  7. Subscribe to the topics with the same callback, each with different timeouts, that will process the subscriptions.
  8. Block the current thread until the current instance receives a signal or the timeout expires.
  9. Dispose the connection to automatically unsubscribe the topics.

Example code

The following example code details creating a callback method to process the subscriptions:

private void PubSubCallback(PubSubMessage message)
{
    try
    {
        // Process the message if there are no errors.
        if (message.Status == PubSubStatus.PS_OK)
        {
            Console.WriteLine(
quot;Topic:
{message.Topic} Value read: {(Double)message.GetValue()}"); } else { Console.WriteLine(
quot;
{message.Topic} failed. Couldn't read the value due to: {message.Status}"); } // Lock the shared resource to make it thread safe. lock (topicList) { topicList.Remove(message.Topic); // Set the state of the event to signal when all of the subscriptions have been processed. if (topicList.Count == 0) { waitHandle.Set(); } } } catch (Exception ex) { Console.WriteLine(
quot;An error occurred:
{ex.Message}"); } }

The following example code details adding multiple subscriptions and processing their callbacks:

internal void AddSeveralSubscriptionsWithCallback()
{
    try
    {
        IPubSubConnectionFactory pubSubConnectionFactory = new PubSubConnectionFactory();
        // Open a PubSub connection.
        using (var pubsubConnection = pubSubConnectionFactory.OpenConnection())
        {
            // Confirm there is connection with the PubSub system.
            if (pubsubConnection.IsConnected())
            {
                var topic1 = "es.realtime.db.analog.TestAnalog1.curval";                       
                var topic2 = "es.realtime.db.analog.TestAnalog2.curval";
                var topic3 = "es.realtime.db.analog.TestAnalog3.curval";
                       
                topicList = new List<String>() { topic1, topic2, topic3 };
                // Global AutoResetEvent waitHandle variable declared out of the scope.
                waitHandle = new AutoResetEvent(false);
                // Subscribe to topics with the same callback, each with different timeouts, that will process the subscription.
                pubsubConnection.Subscribe(topic1, TimeSpan.FromSeconds(15), true, PubSubCallback);
                pubsubConnection.Subscribe(topic2, Timeout.InfiniteTimeSpan, true, PubSubCallback);                                              
                pubsubConnection.Subscribe(topic3, TimeSpan.FromSeconds(30), true, PubSubCallback);                       
                // Block the current thread until the current instance receives a signal or timeout expires.
                waitHandle.WaitOne(TimeSpan.FromSeconds(35));
            }
        }// Dispose the connection to automatically unsubscribe the topics.
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error:" + ex.Message);
    }
}
TitleResults for “How to create a CRG?”Also Available in