C# 调用线程无法访问此对象,因为另一个线程拥有

C# 调用线程无法访问此对象,因为另一个线程拥有,c#,multithreading,dynamic-data-display,C#,Multithreading,Dynamic Data Display,我正在使用dynamicatadisplay进行绘图。我收到一条错误消息:调用线程无法访问此对象,因为当调试器点击以下行时,另一个线程拥有它: Action AddLineGraph = delegate() { timeDomainPlotter.AddLineGraph(_ods, new Pen(_curveColors[_statsEnableIndex[i]], 2), new CirclePointMarker { Size = 5,

我正在使用
dynamicatadisplay
进行绘图。我收到一条错误消息:调用线程无法访问此对象,因为当调试器点击以下行时,另一个线程拥有它:

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);
我对此感到困惑,因为当我使用
LineGraph
绘制绘图时,如下所示:

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

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, AddLineGraph);
它运行良好。所以我想知道为什么绘制点标记会给出错误消息?谢谢

编辑:这里是我在定义
\u curveColors
的地方添加的更多编码

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);

        }
    }
}
下面是没有错误的代码:

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);

在这里,您只能看到“曲线颜色”已更改为“确定性颜色”。

我猜没有在UI线程上创建“曲线颜色”。您应该回溯并查看InitiatePlot是如何被调用的,并确保UI线程拥有_curveColors。

谁拥有_curveColors变量?这似乎是唯一的区别。我在原始线程中添加了更多的代码。曲线颜色的定义就在上面,你说得对。事实上,曲线颜色就是原因。非常感谢。哦,如果你发布你的答案,我会接受你的。
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);