2013-03-11 106 views
1

我正在使用DynamicDataDisplay到图。我得到了一个错误信息:,因为不同的线程拥有它调用线程不能访问该对象当调试器击中以下行:调用线程不能访问此对象,因为不同的线程拥有

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

它运行良好。所以我想知道为什么绘图点标记会给出错误信息?谢谢。

编辑:这里是更多的编码,我添加了_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); 

     } 
    } 
} 

我找到了原因。完全像SpaceghostAli提到的那样,_curveColors确实是导致此错误的罪魁祸首。

我不得不说我没有完全弄清楚为什么,但如果我用定义的颜色替换所有的_curveColors,问题就没有了。特别是,这里是给错误代码:

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

在那里你可以看到只有_curveColors更改为确定性的颜色。

+0

谁拥有_curveColors变量?这似乎是唯一的区别。 – SpaceghostAli 2013-03-11 15:03:23

+0

我在原始线程中添加更多代码。 _curveColors在上面定义。 – 2013-03-11 15:08:49

+0

你说得对。 _curveColors是契据。非常感谢。哦,如果你发布你的答案,我会接受你的答案。 – 2013-03-11 16:12:53

回答

2

我猜测_curveColors没有在UI线程上创建。您应该回溯并查看InitiatePlot如何被调用,并确保UI线程拥有_curveColors。

相关问题