Search code examples
c#multithreadingdynamic-data-display

The calling thread cannot access this object because a different thread owns


I am using DynamicDataDisplay to to the plot. I got a error message: The calling thread cannot access this object because a different thread owns it when debugger hitting the following line:

Action AddLineGraph = delegate()
{    
    timeDomainPlotter.AddLineGraph(_ods,
        new Pen(_curveColors[_statsEnableIndex[i]], 2),
        new CirclePointMarker { Size = 5, Fill = _curveColors[_statsEnableIndex[i]] },
        new PenDescription(Convert.ToString(j)));
};

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, AddLineGraph);

I am confused on this, because when I draw plot using LineGraph as following:

Action AddLineGraph = delegate()
{
    timeDomainPlotter.AddLineGraph(_ods, 2, "Ch" + Convert.ToString(j) + _statsName[_statsEnableIndex[i]]);
};

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, AddLineGraph);

it runs fine. So I am wondering why drawing point markers gives error message? Thanks.

EDIT: Here is more coding I added where _curveColors is defined.

public void InitiatePlot()
{
//  InitializeComponent();

    //   timeDomainPlotter.Legend.Remove();

    _initialChildrenCount = timeDomainPlotter.Children.Count;

    int count = timeDomainPlotter.Children.Count;

    //do not remove the initial children
    if (count > _initialChildrenCount)
    {
        for (int i = count - 1; i >= _initialChildrenCount; i--)
        {
            timeDomainPlotter.Children.RemoveAt(i);
        }
    }


//  _curveColors = new System.Drawing.Color[_nMaxStatsPerChannel];
    _curveColors = new Brush[_nMaxStatsPerChannel];


    for (int i = 0; i < _nMaxStatsPerChannel; i++)
    {

        _curveColors[i] = new SolidColorBrush((Color)ColorConverter.ConvertFromString(_colorList[i]));
    // _curveColors[i] = System.Drawing.Color.FromName(_colorList[i]);

    }

    _statsName = Enum.GetNames(typeof(Window1.ROISignalList));


    //_statsEnableIndex = new int[_nActiveStatsPerChannel];
    for (int j = 0; j < _nActiveChannels; j++)  // init data source structure
    {
    // count = 0;
        for (int i = 0; i < _nActiveStatsPerChannel; i++)
        {

            _ods = new ObservableDataSource<Point>();
            _odsAll[j * _nActiveStatsPerChannel + i] = _ods;   // _osdAll: C0S0 C0S1 C0S2 C1S0 C1S1 C1S2 ... C4S0 C4S1 C4S2


            Action AddLineGraph = delegate()
        {

            timeDomainPlotter.AddLineGraph(_ods,
                    new Pen(_curveColors[_statsEnableIndex[i]], 2),
                    new CirclePointMarker { Size = 5, Fill = _curveColors[_statsEnableIndex[i]] },
                    new PenDescription(Convert.ToString(j)));
        };
        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, AddLineGraph);

        }
    }
}

I found the reason. Exactly like SpaceghostAli mentioned, the _curveColors is indeed the culprit that causes this error.

I have to say I did not completely figure out why, but if I replace all _curveColors with defined color, the problem is gone. Particularly, here is the code that gives error:

Action AddLineGraph = delegate()
{

    timeDomainPlotter.AddLineGraph(_ods,
        new Pen(_curveColors[_statsEnableIndex[i]], 2),
        new CirclePointMarker { Size = 5, Fill = _curveColors[_statsEnableIndex[i]] },
        new PenDescription(Convert.ToString(j)));
};

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, AddLineGraph);

and here is the code without error:

Action AddLineGraph = delegate()
{

    timeDomainPlotter.AddLineGraph(_ods,
        //new Pen(_curveColors[_statsEnableIndex[i]], 2),
        new Pen( new SolidColorBrush(Colors.Transparent), 2),
        new CirclePointMarker { Size = 5, Fill = new SolidColorBrush(Colors.Green) },
        new PenDescription(Convert.ToString(j)));
};

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, AddLineGraph);

where you can see only the _curveColors is changed to deterministic color.


Solution

  • I'm guessing _curveColors isn't being created on the UI thread. You should trace back and see how InitiatePlot is being called and make sure the UI thread owns _curveColors.