c asynchronous read database and asynchronous update ui code implementation

  • 2020-05-26 10:02:48
  • OfStack

Asynchronously read the database, there will be some problems in the data binding, that is, the form interface will not be closed, to end the task to end the process. For example, the following code

First, set the thread to update UI as usual

a2. CheckForIllegalCrossThreadCalls = false; //a2 is the name of the form

The following code takes the data from the database and binds it


private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con;
            SqlCommand com;
            try
            {
                con = new SqlConnection("UID=sa;Password=123;Initial Catalog=AD;Data Source=192.168.1.1;Asynchronous Processing=true");
                con.Open();
                com = new SqlCommand("select top 100 * from tb_user", con);
                com.BeginExecuteReader(new AsyncCallback(delDataBin), com);
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Program error , information : " + ex.Message);
            }
        }
        private void delDataBin(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                SqlCommand com = (SqlCommand)ar.AsyncState;
                SqlDataReader dr = com.EndExecuteReader(ar);
                DataTable dt = new DataTable();
                dt.Load(dr);
                dr.Close();
                this.dataGridView1.DataSource = dt;  // Data binding             
            }
        }

To complete the binding work here, run to see the effect of 1, in fact, this is the window will appear dead phenomenon.

Let's do it with Invoke

First declare public delegate void updateDG(DataTable dt);

DataGridView is then bound via dataBin


        public void dataBin(DataTable dt)
        {
            dataGridView1.DataSource = dt;
            return;
        }  

The following method is called inside the thread


// Data binding 
                if (this.InvokeRequired)
                {
                    updateDG ur = new updateDG(dataBin);
                    this.Invoke(ur, dt);
                }

The complete code is as follows:


        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con;
            SqlCommand com;
            try
            {
                con = new SqlConnection("UID=sa;Password=123;Initial Catalog=AD;Data Source=192.168.1.1;Asynchronous Processing=true");
                con.Open();
                com = new SqlCommand("select top 100 * from tb_user", con);
                com.BeginExecuteReader(new AsyncCallback(delDataBin), com);
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Program error , information : " + ex.Message);
            }
        }
        private void delDataBin(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                SqlCommand com = (SqlCommand)ar.AsyncState;
                SqlDataReader dr = com.EndExecuteReader(ar);
                DataTable dt = new DataTable();
                dt.Load(dr);
                dr.Close();
                //this.dataGridView1.DataSource = dt;// Data binding 
                if (this.InvokeRequired)
                {
                    updateDG ur = new updateDG(dataBin);
                    this.Invoke(ur, dt);
                }
            }
        }

        public delegate void updateDG(DataTable dt);
        public void dataBin(DataTable dt)
        {
            dataGridView1.DataSource = dt;
            return;
        }            

Check the run and look at 1, and you will find the result


Related articles: