Hi Guys.
I am building a project that draw a chart with real time using Microsoft Charts Control . 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.
here is source code http://www.mediafire.com/?3yve3x0l3s8lzb8
Thank You for you time.
Attachment: StockCharts.zip