华东15选5不开奖

c#异步读取数据库与异步更新ui的代码实现

c#异步读取数据库与异步更新ui的代码实现,这篇文章主要介绍了c#从数据库里取得数据并异步更新ui的方法,大家参考使用吧
关键字:异步更新ui、异步读取数据库

异步读取数据库,在数据绑定的时候会出现点问题,就是窗体界面会无法关闭,要结束任务才能结束进程。例如下面代码

首先按习惯的方法,设定线程更新UI

a2.CheckForIllegalCrossThreadCalls = false;  //a2为窗体名称

下面的代码就是从数据库里取得数据并绑定

复制代码 代码如下:

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("程序发生错误,信息: " + 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;  //绑定数据           

            }
        }

到这里完成的绑定的工作,运行查看一下效果,其实这样是会出现窗体假死的现象。

下面通过Invoke 来实现

首先声明委托  public delegate void updateDG(DataTable dt);

然后通过dataBin来绑定DataGridView

复制代码 代码如下:

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

在线程里面调用下面方法

复制代码 代码如下:

//绑定数据
                if (this.InvokeRequired)
                {
                    updateDG ur = new updateDG(dataBin);
                    this.Invoke(ur, dt);
                }

完整的代码如下:

复制代码 代码如下:

        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("程序发生错误,信息: " + 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;//绑定数据

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

查运行查看一下,你就会发现结果了

华东15选5不开奖