Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate Graph while picking values from the database
    primarykey
    data
    text
    <p>I am trying to <strong>pick values from the database and keep updating the graph from there</strong>. Using <a href="https://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c/661662#661662">How to update the GUI from another thread in C#?</a> my code is:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { string myConnection = "datasource=localhost;port=3306;username=root;password=root"; MySqlConnection conDataBase = new MySqlConnection(myConnection); MySqlCommand cmdDataBase = new MySqlCommand(" select * from data.test; ", conDataBase); MySqlDataReader myReader; this.Invoke((MethodInvoker)delegate { try { conDataBase.Open(); myReader = cmdDataBase.ExecuteReader(); while (myReader.Read()) { this.chart1.Series["Series1"].Points.AddXY(myReader.GetString("datetime"), myReader.GetInt32("temp")); } conDataBase.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }); } </code></pre> <p>Though i do not get any error, i dont think its working as the graph is constant/static. Any suggestions. <strong>What i basically want is that this graph keeps updating based on the new values in the database...something like a heart beat monitor or to that effect.</strong> </p> <p>Any suggestions...Regards</p> <p>Edit: I also tried using background worker but there also i get the following on button click:</p> <pre><code>Cross thread operation not valid: Control 'charTemperature' accessed from athread other than the thread it was created on </code></pre> <p>The code:</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; using System.Threading; namespace project { public partial class Form2 : Form { private BackgroundWorker bw = new BackgroundWorker(); public Form2() { InitializeComponent(); bw.WorkerSupportsCancellation = true; bw.WorkerReportsProgress = false; bw.DoWork += new DoWorkEventHandler(bw_DoWork); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void btnTemperature_Click(object sender, EventArgs e) { if (bw.IsBusy != true) { bw.RunWorkerAsync(); } //this.Invoke((MethodInvoker)delegate // { /* string myConnection = "datasource=localhost;port=3306;username=root;password=root"; MySqlConnection conDataBase = new MySqlConnection(myConnection); MySqlCommand cmdDataBase = new MySqlCommand(" select * from data.test; ", conDataBase); MySqlDataReader myReader; try { conDataBase.Open(); myReader = cmdDataBase.ExecuteReader(); while (myReader.Read()) { this.chartTemperature.Series["Temperature"].Points.AddXY(myReader.GetString("datetime"), myReader.GetInt32("temp")); } conDataBase.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } //});*/ } private void btnStopUpdating_Click(object sender, EventArgs e) { if (bw.WorkerSupportsCancellation == true) { bw.CancelAsync(); } } private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; while (true) { if ((worker.CancellationPending == true)) { e.Cancel = true; break; } else { string myConnection = "datasource=localhost;port=3306;username=root;password=root"; MySqlConnection conDataBase = new MySqlConnection(myConnection); MySqlCommand cmdDataBase = new MySqlCommand(" select * from data.test; ", conDataBase); MySqlDataReader myReader; try { conDataBase.Open(); myReader = cmdDataBase.ExecuteReader(); while (myReader.Read()) { this.chartTemperature.Series["Temperature"].Points.AddXY(myReader.GetString("datetime"), myReader.GetInt32("temp")); System.Threading.Thread.Sleep(1000); } conDataBase.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } private void Form2_Load(object sender, EventArgs e) { } } } </code></pre> <p>One more futile attempt...nothing happens on button click...</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; using System.Threading; namespace project { public partial class Form2 : Form { private BackgroundWorker bw = new BackgroundWorker(); public string vdatetime; public Int32 vtemp; public Form2() { InitializeComponent(); bw.WorkerSupportsCancellation = true; // bw.WorkerReportsProgress = false; bw.DoWork += new DoWorkEventHandler(bw_DoWork); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void btnTemperature_Click(object sender, EventArgs e) { //if (bw.IsBusy != true) //{ this.bw.RunWorkerAsync(); //} //this.Invoke((MethodInvoker)delegate // { /* string myConnection = "datasource=localhost;port=3306;username=root;password=root"; MySqlConnection conDataBase = new MySqlConnection(myConnection); MySqlCommand cmdDataBase = new MySqlCommand(" select * from data.test; ", conDataBase); MySqlDataReader myReader; try { conDataBase.Open(); myReader = cmdDataBase.ExecuteReader(); while (myReader.Read()) { this.chartTemperature.Series["Temperature"].Points.AddXY(myReader.GetString("datetime"), myReader.GetInt32("temp")); } conDataBase.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } //});*/ } private void btnStopUpdating_Click(object sender, EventArgs e) { // if (bw.WorkerSupportsCancellation == true) //{ this.bw.CancelAsync(); //} } private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if ((worker.CancellationPending == true)) { e.Cancel = true; //break; } else { string myConnection = "datasource=localhost;port=3306;username=root;password=root"; MySqlConnection conDataBase = new MySqlConnection(myConnection); MySqlCommand cmdDataBase = new MySqlCommand(" select * from data.test; ", conDataBase); MySqlDataReader myReader; try { conDataBase.Open(); myReader = cmdDataBase.ExecuteReader(); //this.Invoke((MethodInvoker)delegate //{ while (myReader.Read()) { vdatetime = myReader.GetString("datetime"); vtemp = myReader.GetInt32("temp"); //Thread.Sleep(300); // this.chartTemperature.Series["Temperature"].Points.AddXY(myReader.GetString("datetime"), myReader.GetInt32("temp")); // System.Threading.Thread.Sleep(1000); } conDataBase.Close(); // }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { // The user canceled the operation. MessageBox.Show("Operation was canceled"); } else if (e.Error != null) { // There was an error during the operation. string msg = String.Format("An error occurred: {0}", e.Error.Message); MessageBox.Show(msg); } else { this.chartTemperature.Series["Temperature"].Points.AddXY(vdatetime, vtemp); } } private void Form2_Load(object sender, EventArgs e) { } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload