Pre loader

Problem - drawing the latest 300 points of a real time generated sine wave on the fly on a background 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

1
0

Hello,

I am now experimenting with SciChart (trail period).
I want to constantly draw the latest 300 points of a real time generated sine wave. I wrote a little program that tries to do this (attached). The sine wave is generated in the OnTimer function. I expected Draw_Limited_Range() function to draw the latest 300 points using XAxis.VisibleRange. Unfortunately this doesn’t work from OnTimer. Same function does work as expected when called from a button (Set VisableRange).
The output window of the debugger keeps displaying following message: A first chance exception of type ‘System.InvalidOperationException’ occurred in WindowsBase.dll

I tried this both on Ver 3.2 and 3.1; On 3.1 it crashes very fast.

What am I doing wrong?

Thanks,

Danny

  • You must to post comments
1
0

Hi Danny,

Without being able to debug the code, we can’t tell I’m afraid. However it sounds to me like you have a cross-thread issue.

SciChart itself is a very thread-safe user control. Most WPF UserControls can only ever be accessed from the UI thread. With SciChart, you can append or modify data (DataSeries API) on a background thread. You can control certain pairs of the API from background threads.

Unfortunately, VisibleRange is a DependencyProperty and WPF requires that DP’s are set from the UI Thread.

We work around this in our examples by using MVVM. In MVVM, data bindings can be updated in ViewModels from the background thread and WPF will automatically marshal the binding to the UI Thread.

The rule of thumb is (and this is a WPF restriction, not a SciChart one)

  • Any FrameworkElement derived class must be instantiated on, and manipulated from the UI thread
  • Any non-FE derived class may be instantiated on and manipulated from any thread
  • DependencyProperties (on DependencyObject) must be updated on a UI Thread
  • You may update Viewmodel bindings on a background thread because WPF automatically marshals bindings to dependency properties back onto the UI Thread

Best regards,
Andrew

  • Danny Ellenbogen
    Hi Andrew, Thanks for your quick response. I actually thought about this and early this morning replaced the timer with a .NET dispatcherTimer. That did the trick and for the past 3 hours the graph is plotting as expected. I will keep playing with the component, may try your suggestion using MVVM. Thanks, Danny
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies