Click here to Skip to main content
14,386,394 members
Rate this:
Please Sign up or sign in to vote.
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);
  tHOSEIDX.Start();

 }
}



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);
   myConnection.Open();
   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";
   VnIndexChart.DataBind();
   UpdateChartSettings();


  }



Help me fix the error. Sorry for my english.

here is source code http://www.mediafire.com/?3yve3x0l3s8lzb8

Thank You for you time.
Posted
Updated 11-Aug-10 19:41pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Time to go back to the thread basics...

http://www.albahari.com/threading/part2.aspx[^]

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!
   
Comments
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.
http://www.codeproject.com/KB/cs/workerthread.aspx
http://msdn.microsoft.com/en-us/library/757y83z4%28VS.71%29.aspx

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100