0

我正在尝试从数据库中选择值并从那里不断更新图表。使用如何从 C# 中的另一个线程更新 GUI?我的代码是:

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);
                    }
                });
        }

虽然我没有收到任何错误,但我不认为它的工作作为图表是恒定/静态的。有什么建议么。我基本上想要的是该图表根据数据库中的新值不断更新……类似于心跳监视器或类似的效果。

任何建议...问候

编辑:我也尝试使用后台工作人员,但在按钮单击时我也得到以下信息:

Cross thread operation not valid: Control 'charTemperature' accessed from athread other than the thread it was created on

编码:

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)
        {

        }


    }
}

又一次徒劳的尝试……点击按钮没有任何反应……

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)
        {

        }

    }
}
4

2 回答 2

0

假设您无法将更改通知推送到客户端,您可能需要某种轮询方法。

您可以设置一个计时器(请参阅下面的链接)并以经过的时间间隔查询新数据。
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

我不确定您使用的是哪种图表,但您可以在系列末尾添加新点(并从系列前面删除旧点),这可能会给您带来心跳之类的东西你提到的监控效果......(如果图表不支持你可能需要重建它,或者在每次更新时给它一个全新的系列)

您可能想要调整您正在使用的查询,以便在每次调用时仅获得新数据点。(其中 datetime > [maximum datetime already retrieved in a previous call])

如您的问题中所述,仍然可以通过调用(..)调用实际执行 UI 更新的代码...

于 2013-07-17T13:22:54.083 回答
0

好的,这样做:

使用系统;使用 System.Collections.Generic;使用 System.ComponentModel;使用 System.Data;使用 System.Drawing;使用 System.Linq;使用 System.Text;使用 System.Windows.Forms;使用 MySql.Data.MySqlClient;使用 System.Threading;

命名空间项目{公共部分类Form2:表格{

    public delegate void AddGraphPointsTemp();
    public AddGraphPointsTemp myDelegate1;
    Thread tempThread;
    string myConnection;
    MySqlConnection conDataBase;
    MySqlCommand cmdDataBase;
    MySqlDataReader myReader;


    public Form2()
    {
        InitializeComponent();

        myDelegate1 = new AddGraphPointsTemp(AddGraphPointsMethodTemp);
        myConnection = "datasource=localhost;port=3306;username=root;password=root";
        conDataBase = new MySqlConnection(myConnection);
        cmdDataBase = new MySqlCommand(" select * from data.test; ", conDataBase);
        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();


    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        conDataBase.Close();
        Application.Exit();
    }

    public void AddGraphPointsMethodTemp()
    {
        try
        {

                myReader.Read();
                chartTemperature.Series["Temperature"].Points.AddXY(myReader.GetString("datetime"), myReader.GetInt32("temp"));
                chartTemperature.Update();




        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }

    private void btnTemperature_Click(object sender, EventArgs e)
    {
        tempThread = new Thread(ThreadFunction1);
        tempThread.Start(this);

    }


    public static void ThreadFunction1(Object obj)
    {
        while (true)
        {
            Form2 myForm2 = (Form2)obj;
            myForm2.Invoke(myForm2.myDelegate1);
            Thread.Sleep(300);
        }
    }


    private void Form2_Load(object sender, EventArgs e)
    {

    }

}

}

于 2013-07-19T04:32:51.220 回答