We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Doesn't handle multi threads properly

Thread ID:

Created:

Updated:

Platform:

Replies:

143008 Feb 28,2019 11:10 AM UTC Mar 28,2019 11:21 AM UTC WPF 5
loading
Tags: SfChart
Tom
Asked On February 28, 2019 11:10 AM UTC

There is a bug in the charts databinding to a observable collection and using EnableCollectionSynchronization - which enables updating the collection from another thread without having to call the dispatcher. This works fine binding the list to a standard wpf datagrid, but the RaisePropertyChange throws the below exception when the collection is bound to a chart and ListenForPropertyChanges is set to true. The chart is probably not locking the collection as required to access.

System.InvalidOperationException
  HResult=0x80131509
  Message=The calling thread cannot access this object because a different thread owns it.
  Source=WindowsBase
  StackTrace:
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at Syncfusion.UI.Xaml.Charts.ChartSeriesBase.SetIndividualPoint(Int32 index, Object obj, Boolean replace)
   at Syncfusion.UI.Xaml.Charts.ChartSeriesBase.OnDataCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.SetItem(Int32 index, T item)

Muneesh Kumar G [Syncfusion]
Replied On February 28, 2019 01:07 PM UTC

Hi Tom, 
 
Greetings, we have analyzed your query and you can resolve this by using Dispatcher as per the below code snippet.  
 
Code snippet 
private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            this.Dispatcher.Invoke(() => 
            { 
                EditTheCollection(); 
            }); 
        } 
 
        public void EditTheCollection() 
        { 
            viewModel.Data[0].YValue=10; 
        } 
    } 
 
    public class Model :INotifyPropertyChanged 
    { 
        public double XValue { get; set; } 
        private double yValue; 
 
        public double YValue 
        { 
            get { return yValue; } 
            set { yValue = value; OnPropertyChanged("YValue"); } 
        } 
 
 
        public event PropertyChangedEventHandler PropertyChanged; 
 
        private void OnPropertyChanged(string name) 
        { 
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 
        } 
    } 
 
    public class ViewModel 
    { 
        public ViewModel() 
        { 
            GenerateData();            
        } 
 
        private static object _lock = new object(); 
 
        public void GenerateData() 
        { 
            Data = new ObservableCollection<Model>(); 
            Random rd = new Random(); 
            for (int i = 0; i < 6; i++) 
            { 
                Data.Add(new Model() 
                { 
                    XValue = i, 
                    YValue = rd.Next(0, 50) 
                }); 
            } 
 
           BindingOperations.EnableCollectionSynchronization(Data, _lock); 
        } 
 
        private ObservableCollection<Model> data; 
 
        public ObservableCollection<Model> Data 
        { 
            get { return data; } 
            set { data = value;  } 
        } 
 
    } 
 
 
Please refer below link for more details.  
 
Hope it helps you.  
 
Thanks, 
Muneesh Kumar G.  
 


Tom
Replied On February 28, 2019 06:18 PM UTC

Yes but the point is that using EnableCollectionSynchronization is meant to alleviate the need to call the Dispatcher. For example I might update my collection 10 times elsewhere and now everytime I would have to add the dispatcher just so the chart works. If it was bound to a wpf datagrid instead this isn't needed so somewhere the chart is not handling the synchronization context correctly.

Muneesh Kumar G [Syncfusion]
Replied On March 1, 2019 12:35 PM UTC

Hi Tom,  
 
We have analyzed the reported problem with our source, it occurs due to non-UI thread trying to access the UI thread in our source.  
 
So, we recommend, you to use the solution provided in our previous update.  
 
Thanks, 
Muneesh Kumar G. 


Marcus Edstrand
Replied On March 28, 2019 10:20 AM UTC

Hi Tom,  
 
We have analyzed the reported problem with our source, it occurs due to non-UI thread trying to access the UI thread in our source.  
 
So, we recommend, you to use the solution provided in our previous update.  
 
Thanks, 
Muneesh Kumar G. 


I'm also suffering from this inconsistent behavior compared to e.g. the data grid. I would call it a bug. Why can't it work in the same fashion as for the data grid?

Regards,
Marcus

Muneesh Kumar G [Syncfusion]
Replied On March 28, 2019 11:21 AM UTC

Hi Marcus, 
 
We are unable to get exact data update handling in data grid. But based on our source analysis, whenever you update your UI elements from a thread other than the main thread, you need to use Dispatcher.Invoke. Please check the below link for more details.  
 
  
Regards,  
Muneesh Kumar G. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;