I have a lot of data to process and I don’t want UI to get frozen. So I’d like to add data in a background thread.
Is it possible to append data to XyDataSeries in a separate thread? Is it supposed to handle this safely?
- Anton Kochepasov asked 9 years ago
- You must login to post comments
Hi Anton,
Yes it is. DataSeries is fully thread safe, and decoupled from the drawing of the SciChartSurface. From our article Creating a Real-Time WPF Chart:
SCICHART employs on-demand rendering and optionally allows
multi-threaded data-appending to DataSeries. What this means is, data
can be appended to a DataSeries as fast as necessary and on a
background thread if desired, while the chart will only draw changes
when they are available and when the UI thread is free.SCICHART doesn’t render continuously, but only when a change in the
data has occurred, thus making more efficient use of the CPU. SCICHART
also listens to property changes on the chart itself, so updating the
color of a series, or the VisibleRange of an axis will also trigger a
redraw.This concept is nothing new, and has been used in computer games to
create a game-loop where rendering and processing are performed on
different threads.
Each DataSeries has a SyncRoot object, which is locked whenever an operation like Append, Update, Insert, Remove is called.
When a DataSeries is accessed during the drawing phase, the same SyncRoot is locked by the SciChartSurface to ensure you are not drawing at the same time as updating data.
If you wish to lock the entire surface (suspend drawing temporarily) while making data modifications, you can also do this:
var dataSeries = new XyDataSeries<double, double>();
// .. assumes dataSeries is attached to a SciChartSurface RenderableSeries
// This freezes the parent surface
using (dataSeries.SuspendUpdates())
{
dataSeries.Append(..., ...); // do any operation here
dataSeries.Append(..., ...);
dataSeries.Append(..., ...);
}
// Parent surface is unfrozen and redraws once
Limitations
-
When appending Data in a background thread, you cannot share a DataSeries between more than one SciChartSurface. You can still share a DataSeries between more than one RenderableSeries.
-
When appending data in a background thread, because of thread synchronization, it is advisable to append in blocks to reduce contention between threads.
See also:
- Andrew Burnett-Thompson answered 9 years ago
- You must login to post comments
Please login first to submit.