When I tried to create my DataSeries on a background thread, then call Dispatcher.BeginInvoke() to update the graph on the UI, I'm getting, "The calling thread cannot access this object because a different thread owns it." If I remove the series.Defintion and have the graph draw as a bar graph instead of a line graph, there are no problems whatsoever.
I'm supposed to be showing this at a trade show tomorrow and I'm stuck. It appears that when I specify a LineSeriesDefinition, a new thread internal to the control is introduced into the equation and it's wreaking havoc. Help!
Below is some of the code.
StartUpdatingCharts() is called from a timer:
protected void StartUpdatingCharts(object dummy)
{
foreach (object currentGraph in ActiveGraphs)
{
Thread t = new Thread(UpdateChartInBackground);
t.Start(currentGraph);
}
}
public void UpdateChartInBackground(Object threadContext)
{
GraphControl currentGraph = threadContext as GraphControl;
FillChartWithData(currentGraph);
}
FillChartWithData() is called from a background thread:
protected override void FillChartWithData(GraphControl graphControl)
{
Dictionary<string, double> returnValues = Msi.GetReservoirData(graphControl.Arguments);
DataSeries series = new DataSeries();
series.Definition.ShowItemLabels = false;
LineSeriesDefinition definition = new LineSeriesDefinition();
definition.ShowItemLabels = false;
definition.ShowPointMarks = false;
//series.Definition = definition; //THIS IS THE LINE THAT CAUSES ALL OF THE PROBLEMS!
foreach (string sectionName in returnValues.Keys)
{
series.Add(new DataPoint(returnValues[sectionName]));
}
ChartArea chartArea = graphControl.RadChart.DefaultView.ChartArea;
chartArea.Dispatcher.BeginInvoke(new UpdateChart(BindDataToChart), chartArea, series);
}
private delegate void UpdateChart(ChartArea chartArea, DataSeries series);
private static void BindDataToChart(ChartArea chartArea, DataSeries series)
{
chartArea.DataSeries.Clear();
chartArea.DataSeries.Add(series);
}