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. Image for the cookie policy date

Doesn't handle multi threads properly

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)

5 Replies

MK Muneesh Kumar G Syncfusion Team 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.  
 



TO Tom 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.


MK Muneesh Kumar G Syncfusion Team 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. 



ME Marcus Edstrand replied to Muneesh Kumar G 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


MK Muneesh Kumar G Syncfusion Team 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. 


Loader.
Live Chat Icon For mobile
Up arrow icon