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.
Move the cursor above the event-handler.
Add the following window definition code:
private SubscriptionOutput
outputWindow;
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();
}
outputWindow.Show();
We now need to add the actual event handler for the monitoredItem.Notification defined in Step 3.
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();
}
You are done!
The code in this example is not perfect, and is missing some important
things such as:
Sufficient error trapping.
Removal of monitored items and the subscription.
Next, see Step 5 - Testing your Client.