Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

ObjectDisposedException with ChartCollection an WPF Graph

Solved!
Go to solution

When I'm starting the following program (WPF .net4.0, NI.common (13.0.40.190), NI.Controls.Graph (13.0.40.242) ) I'm getting the following error message from the debugger

 

 

ObjectDisposedException was unhandled

The data store was modified.
Object name: 'Buffer<Int32>'.

Source: NI.Common

 

 

I have a device class that generates every few milli seconds some random data. I notify my main class via Events and pass the values via paramter.

 

In XAML I bind the graph data to a ChartCollection as follows: (the bold lines are most relevant)

 

The error occurs only when the timing of my data provider gets over a certain treshold.

 

500ms - all is fine

200ms - runs ok, but crashes with exception when I change  the window size

20ms - still ok

10ms - crashes instantly with the exception

 

{

        Data data = new Data();


        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private ChartCollection<double> _chartCollection1 = new ChartCollection<double>(1000);
        private ChartCollection<double> _chartCollection2 = new ChartCollection<double>(1000);
        private ChartCollection<double> _chartCollection3 = new ChartCollection<double>(1000);
        private ChartCollection<double> _chartCollection4 = new ChartCollection<double>(1000);

        public ChartCollection<double> Graph1
        {
            get { return _chartCollection1; }
            set
            {
                if (_chartCollection1.Equals(value))
                {
                    return;
                }

                _chartCollection1 = value;
                OnPropertyChanged("Graph1");
            }
        }
        public ChartCollection<double> Graph2
        {
            get { return _chartCollection2; }
            set
            {
                if (_chartCollection2.Equals(value))
                {
                    return;
                }

                _chartCollection2 = value;
                OnPropertyChanged("Graph2");
            }
        }
        public ChartCollection<double> Graph3
        {
            get { return _chartCollection3; }
            set
            {
                if (_chartCollection3.Equals(value))
                {
                    return;
                }

                _chartCollection3 = value;
                OnPropertyChanged("Graph3");
            }
        }
        public ChartCollection<double> Graph4
        {
            get { return _chartCollection4; }
            set
            {
                if (_chartCollection4.Equals(value))
                {
                    return;
                }

                _chartCollection4 = value;
                OnPropertyChanged("Graph4");
            }
        }


        public MainWindow()
        {
            InitializeComponent();

            GraphView graphView = new GraphView();
            ContentControlGraph.Content = graphView;
            ContentControlGraph.DataContext = this;

            data.dataReadyEvent += dataEventHandler;
            Closing += MainWindow_Closing;
        }

        void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            data.Stop();
            Environment.Exit(0);
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
                data.Start();
        }

        private int j;
        private void dataEventHandler(object sender, EventArgs e)
        {
            j++;
            if (j == 1)
            {
                Graph1.Append((double[])sender);
            }
            else if (j == 2)
            {
                Graph2.Append((double[])sender);
            }
            else if (j == 3)
            {
                Graph3.Append((double[])sender);
            }
            else 
            {
                Graph4.Append((double[])sender);
                j = 0;
            }
        }
    }

The exception occurs in the DLL, so I cant catch it either.

 

0 Kudos
Message 1 of 2
(3,844 Views)
Solution
Accepted by topic author Treckl

I believe you are running into the multi-threading issue described here: Can't get Graph.DataSource to work in WPF i (threading issue?).


In short, your chart append calls should be marshaled to the UI thread.

~ Paul H
Message 2 of 2
(3,835 Views)