Pre loader

Is XyDataSeries safe to being changed in a separate thread?

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

Answered
1
0

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?

  • You must to post comments
Best Answer
1
0

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.

enter image description here

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

  1. 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.

  2. 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:

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.