当前位置: 动力学知识库 > 问答 > 编程问答 >

multithreading - c# winform chart created in public and updated on even triggered - cross thread error

问题描述:

I've got a chart that I've created and formatted (no data yet) in the public form, right after the initcomponent().

the series is populated upon event triggered (camera firing a imageavailable event), at that point I gather some pixels and fill the series with data.

This should work fine but I get a "cross thread error", saying that my chart was created in a different thread.

here's what my code looks like:

public Form1()

{

InitializeComponent();

CreateStarProfileChart();

.../...

here's my function:

private void CreateStarProfileChart()

{

// Y axis init

StarProfile.ChartAreas.Add("Area1");

StarProfile.ChartAreas["Area1"].AxisY.Title = "Pixel Values";

StarProfile.ChartAreas["Area1"].AxisY.Minimum = 0;

StarProfile.ChartAreas["Area1"].AxisY.Maximum = 4096;

StarProfile.ChartAreas["Area1"].AxisY.Interval = 500;

StarProfile.ChartAreas["Area1"].AxisY.MajorGrid.Enabled = true;

StarProfile.ChartAreas["Area1"].AxisY.MajorGrid.LineColor = Color.Gray;

StarProfile.ChartAreas["Area1"].AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;

StarProfile.ChartAreas["Area1"].AxisY.MinorTickMark.Enabled = true;

// X axis init

StarProfile.ChartAreas["Area1"].AxisX.Title = "Pixels Accross Centroid";

StarProfile.ChartAreas["Area1"].AxisX.MajorGrid.Enabled = true;

StarProfile.ChartAreas["Area1"].AxisX.MajorGrid.LineColor = Color.Gray;

StarProfile.ChartAreas["Area1"].AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;

StarProfile.ChartAreas["Area1"].AxisX.MinorTickMark.Enabled = true;

// Series init

StarProfile.Series.Add("StarProfile");

StarProfile.Series["StarProfile"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;

StarProfile.Series["StarProfile"].BorderWidth = 2;

StarProfile.Series["StarProfile"].Color = Color.Black;

}

and finally, here's the code in the event that's triggered every few seconds:

// Clear Chart and Re-init Chart

StarProfile.Series["StarProfile"].Points.Clear();

StarProfile.ChartAreas["Area1"].AxisX.Minimum = -BoxDim;

StarProfile.ChartAreas["Area1"].AxisX.Maximum = BoxDim;

StarProfile.ChartAreas["Area1"].AxisX.Interval = 5;

for (int sl = -BoxDim; sl <= BoxDim; sl++)

{

// Add points to chart

StarProfile.Series["StarProfile"].Points.AddXY(sl, PixelMap[CoordX + sl, CoordY]);

../.. // some more maths following but that's all that's related to the chart itself...

any idea how I could do what I want to do?

thanks

Steve

网友答案:

Put your update code into a method like this (it probably already is, but you haven't posted it...):

void RefreshChart()
{
   // Clear Chart and Re-init Chart                 
   StarProfile.Series["StarProfile"].Points.Clear();              
   StarProfile.ChartAreas["Area1"].AxisX.Minimum = -BoxDim;

   ...
}

Then, from your code (assumedly running in a background thread) which is calling RefreshChart(), call it like this, using an anonymous delegate:

Invoke((Action)(() => RefreshChart()));

This should eliminate your error, by marshalling the function call back to the main UI thread.

(Note that in this particular case, you can simplify the call further using method group syntax:)

Invoke((Action)RefreshChart);
分享给朋友:
您可能感兴趣的文章:
随机阅读: