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
- Danny Ellenbogen asked 10 years ago
- last edited 10 years ago
- You must login to post comments
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
- Andrew Burnett-Thompson answered 10 years ago
- 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 login to post comments
Please login first to submit.