Client Development Tutorial

Step 4 - Consuming Data-changes

We will now add the code to consume the data-changes for our monitored item and output them to the screen.

This step is a continuation from Step 3 - Setup Subscription.

Defining our Window Instance

  1. Move the cursor above the event-handler.

  2. Add the following window definition code:
    private SubscriptionOutput outputWindow;

  3. Place the cursor back in the event-handler, but add the following code to the bottom of the code block:
    if( outputWindow == null )
    {
     outputWindow = new SubscriptionOutput();
    }

Directing Data-Change Output to Window

  1. We now need to add the actual event handler for the monitoredItem.Notification defined in Step 3.

  2. Move the cursor out of the event handler and then add the following code:
    void monitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
    {
     if(this.InvokeRequired)
     {
       this.BeginInvoke(new MonitoredItemNotificationEventHandler(monitoredItem_Notification), monitoredItem, e);
       return;
     }
     MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
     if( notification == null )
     {
       return;
     }
     outputWindow.label1.Text = "value: " + Utils.Format( "{0}", notification.Value.WrappedValue.ToString()) +
       ";\nStatusCode: " + Utils.Format( "{0}", notification.Value.StatusCode.ToString()) +
       ";\nSource timestamp: " + notification.Value.SourceTimestamp.ToString() +
       ";\nServer timestamp: " + notification.Value.ServerTimestamp.ToString();
    }

  3. You are done!
    The code in this example is not perfect, and is missing some important things such as:

    1. Sufficient error trapping.

    2. Removal of monitored items and the subscription.

Next, see Step 5 - Testing your Client.

See Also