DataChanged Event
OpcUaSubscription.DataChanged Events are triggered by server-side push notifications when data MI changes.
public event EventHandler<DataChangeEventArgs> DataChanged;
- See the Global Unified Channel
ua.Events.Any(FilterCategory == OpcUaEventCategory.DataChangeby ). - For OPC UA alarms / conditional events (protocol events), see Event Subscription.
DataChangeEventArgs
| Field | Type | Note |
|---|---|---|
| MonitoredItemHandle | uint | Triggered MI Local Handle |
| NodeId | string | It's about time MI NodeId |
| ValueString | string | String representation of the value (provided during the callback; thread-safe) |
| DataTypeName | string | Names of built-in data type enumerations (e.g., Double, Int32) |
| Status | StatusCode | DataValue Status |
| SourceTimestamp | DateTime? | Data Source Timestamp |
| ServerTimestamp | DateTime? | Server Timestamp |
Fields and Original Values
DataChangeEventArgs The provided data is a flat field (string + enumeration + DateTime?), which can be used across threads with zero risk, at the cost ofCan't access the original data Variant Perform advanced operations.
If you need the original ( Variant e.g., for parsing complex data ExtensionObject), use it to re-read the data synchronously ua.Read(e.NodeId) in the callback (be DataValue sure to Dispose the returned object).
Examples
Simple Log
sub.DataChanged += (s, e) =>
Console.WriteLine($"{e.NodeId} = {e.ValueString} ({e.DataTypeName}) @ {e.SourceTimestamp}");
Write to a file / database
using System.IO; // File.AppendAllText
sub.DataChanged += (s, e) =>
{
lock (_writeLock)
{
File.AppendAllText("data.csv",
$"{e.SourceTimestamp:O},{e.NodeId},{e.ValueString}\n");
}
};
UI Bind (WPF)
sub.DataChanged += (s, e) =>
{
Dispatcher.Invoke(() =>
{
labelTemp.Content = e.ValueString;
labelStatus.Foreground = e.Status == StatusCode.Good
? Brushes.Green : Brushes.Red;
});
};
Otherwise, cross-thread operations UI will throw an exception InvalidOperationException.
Thread Model
- The callback runs on the Publish thread SDK in the background (not the
thread UI); all fields are already available when the callback is triggered, making it thread-safe. - Exceptions thrown in the callback are caught SDK by, It will not spread to UI threads, nor will it interrupt subsequent notifications, andIt won't be recorded in the log file. — If you want to log the callback, use a try/catch block within the callback to record it yourself.
- The SDK does not specify whether "callbacks within one subscription are serialized or callbacks across subscriptions run concurrently" — treat every case as potentially concurrent: lock shared data yourself
- It is recommended that callbacks only handle forwarding or enqueuing, and avoid time-consuming operations (writing to files, querying databases, and cross-thread Invoke calls should all be moved to their own worker threads).
Relationship with the Unified Event Channel
// Option A: Single Subscription Tier
sub.DataChanged += (s, e) => /* ... */;
// Method B: Global Unified Channel (All Subscriptions Consolidated) — No DataChange dedicated events; uses "Any" filtered by category
ua.Events.Any += (s, entry) =>
{
if (entry.Category != OpcUaEventCategory.DataChange) return;
// entry.NodeId / entry.Message (= value string) / entry.StatusCode
};
Both rules will be triggered for the same data point (first exit DataChanged, then enter the unified channel); simply choose one to use.
In the unified channel, the entries DataChange are OpcUaEventEntry, and only NodeId / Message(value string) / StatusCode are available, NoneDataTypeName / Timestamp — Use Method A for these fields.