Skip to main content

Read History Stream (ReadHistoryStreamAsync)

Overview​

IDE Trend Chart / When fetching historical data for a historical HMI curve, loading tens of thousands of data points all at once List<DataValue> will cause UI thread stuttering + OOM.ReadHistoryStreamAsync Returns IAsyncEnumerable<DataValue>; on the business side, use await foreach to retrieve one data point at a time and process it as it is fetched, rendering as you go.

Corresponding section of the standard: OPC UA Part 11(Historical Access) §6.5 (HistoryRead).

API​

MethodsCategoryReading and WritingNote
ua.ReadHistoryStreamAsync(nodeId, startUtc, endUtc, batchSize, cancellationToken)HistoryReadAsynchronous, stream-based reading of raw historical data

DarraOpcUa The extension method is defined in DarraOpcUa_Client.HistoryStreamExtensions:

public static async IAsyncEnumerable<DataValue> ReadHistoryStreamAsync(
this DarraOpcUa ua,
string nodeId,
DateTime startUtc,
DateTime endUtc,
uint batchSize = 1000,
[EnumeratorCancellation] CancellationToken cancellationToken = default);

Parameters:

ParametersTypeDefaultNote
nodeIdstring—Historical Milestones NodeId
startUtcDateTime—Start UTC
endUtcDateTime—End UTC
batchSizeuint1000RPC Maximum number of points per attempt (subject to server availability)
cancellationTokenCancellationTokendefaultRevoke Token

Return: IAsyncEnumerable<DataValue>, each element Must Dispose (Implementation IDisposable).

Code Example​

using System;
using System.Linq; // CountAsync Must be installed separately System.Linq.Async
using System.Threading;
using System.Threading.Tasks;
using DarraOpcUa_Client;

using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Connect();

var t0 = DateTime.UtcNow.AddHours(-24);
var t1 = DateTime.UtcNow;

// 1) Render as you go
await foreach (var dv in ua.ReadHistoryStreamAsync("ns=2;s=Temp", t0, t1))
{
using (dv)
{
double y = dv.Value.AsDouble ?? double.NaN; // AsDouble Is it a double? If not, it is null.
chartSeries.Points.Add(dv.SourceTimestamp ?? DateTime.UtcNow, y);
}
}

// 2) Discontinue support
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
try
{
await foreach (var dv in ua.ReadHistoryStreamAsync("ns=2;s=Temp", t0, t1, cancellationToken: cts.Token))
using (dv) Process(dv);
}
catch (OperationCanceledException)
{
Console.WriteLine("超时, 已部分加载");
}

// 3) LINQ Style Filter (System.Linq.Async)
var bad = await ua.ReadHistoryStreamAsync("ns=2;s=Temp", t0, t1)
.Where(dv => dv.Status != StatusCode.Good)
.CountAsync();
Console.WriteLine($"24h 内有 {bad} 个坏值");

Implementation Constraints​

  • Currently, ReadHistory no points are exposed ContinuationPoint; a single call RPC returns all points within the interval (subject to restrictions batchSize ).
  • batchSize This is just a hint regarding "the maximum number of items a caller can retrieve at a time"; whether it actually takes effect depends on the server.
  • For large-interval operations, the business side must perform its own slicing—for example, dividing 24 hours into 24 segments of 1 hour and processing them sequentially. await foreach
  • Remove the two-part structure: RPC Hasn't started yet Cancel the throw at that time OperationCanceledException; once it has started running RPC, it cannot be interrupted (the result is returned normally). Canceling after that results in a clean stop—no exception is thrown—but it simply stops yielding the points that follow.

Comparison with ReadHistory​

// Synchronization: Gain tens of thousands of points at once, UI pause for 1 second
var vals = ua.ReadHistory("ns=2;s=Temp", t0, t1, maxValues: 50000);
foreach (var dv in vals)
using (dv) chartSeries.Points.Add(...);

// Asynchronous Streaming: Render as you load, always UI responsive
await foreach (var dv in ua.ReadHistoryStreamAsync("ns=2;s=Temp", t0, t1))
using (dv) chartSeries.Points.Add(...);

Best Practices​

  • Must be for each using dv — Forgetting to call Dispose will cause a memory leak
  • Revoke Token — Provide users with a "Cancel" button for long queries
  • Segmentation Strategy — For intervals longer than 1 hour, it is recommended to manually slice the outer layer to prevent truncation caused by the server's single-response limit.
  • UI Render Throttling — Call it once every 100 entries chart.Refresh(); don't refresh after every entry.

Cross-Language Comparison​

C#PythonJavaC++RustC
ReadHistoryStreamAsyncread_history_streamreadHistoryStreamReadHistoryStreamread_history_streamDarraUa_HistoryStream_New/_Next/_IsEnd/_Free

Next Step​