Click here to Skip to main content
15,393,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys.

I am building a project that draw a chart with real time using Microsoft Control Charts. Data source of chart changes every a few seconds to reload data. but i have got a trouble with it. The error is '{"Cross-thread operation not valid: Control...'

I use thread job to start thread every time when checking thread is complete.

private void gLoads()

   jHOSEIDX = new ThreadStart(LoadChart);
   tHOSEIDX = new Thread(jHOSEIDX);
   GTimer.Interval = 10000; // 10 seconds 
   GTimer.Tick += new EventHandler(Otherwise);
   GTimer.Enabled = true;

Here is otherwise method.
private void Otherwise(object sender, EventArgs e)
 if (!(tHOSEIDX.ThreadState.ToString().CompareTo("Running") == 0))
  jHOSEIDX = new ThreadStart(LoadChart);
  tHOSEIDX = new Thread(jHOSEIDX);


Loadchart is a method reload data and adjust properties of chart.
private void LoadChart()

    VnIndexChart.Titles[0].Text = "VN-Index";
   string fileNameString = AppDomain.CurrentDomain.BaseDirectory + @"\Data\Data.mdb";

   string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;

   string mySelectQuery = "SELECT TGian,Data.Ngay, Data.ChiSoHTai, Data.ChiSoGLap,Data.KLDuMua FROM Data Order By 1; ";
   OleDbConnection myConnection = new OleDbConnection(myConnectionString);
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
   VnIndexChart.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
   VnIndexChart.Series["VN-Index"].XValueMember = "TGian";
   VnIndexChart.Series["VN-Index"].YValueMembers = "ChiSoHTai";
   VnIndexChart.Series["VN-Index"].XValueType = ChartValueType.Time;

   VnIndexChart.Series["VN-IDX.ESTIMATE"].XValueMember = "TGian";
   VnIndexChart.Series["VN-IDX.ESTIMATE"].XValueType = ChartValueType.Time;
   VnIndexChart.Series["VN-IDX.ESTIMATE"].YValueMembers = "ChiSoGLap";

   VnIndexChart.Series["Qlity"].XValueMember = "TGian";
   VnIndexChart.Series["Qlity"].YAxisType = AxisType.Secondary;
   VnIndexChart.Series["Qlity"].XValueType = ChartValueType.Time;
   VnIndexChart.Series["Qlity"].YValueMembers = "KLDuMua";


Help me fix the error. Sorry for my english.

here is source code

Thank You for you time.
Updated 11-Aug-10 18:41pm

1 solution

Time to go back to the thread basics...[^]

Look for: Rich Client Applications and Thread Affinity

You need to synchronize your calls to the UI thread using invoke or otherwise you'll get that error.

Good luck!
huynhdangthai 13-Aug-10 2:37am
Thanks Nijboer. I read above link but don't know where to start resolving my problems.
E.F. Nijboer 13-Aug-10 3:37am
To resolve your problem you have to use the method Invoke to go from your thread to updating the main form. There are some more examples about this,here are some additional links.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900