2013-03-11 111 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。

相關問題