欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#異步讀取數(shù)據(jù)庫與異步更新ui的代碼實(shí)現(xiàn)

 更新時(shí)間:2013年12月09日 10:11:12   作者:  
這篇文章主要介紹了c#從數(shù)據(jù)庫里取得數(shù)據(jù)并異步更新ui的方法,大家參考使用吧

異步讀取數(shù)據(jù)庫,在數(shù)據(jù)綁定的時(shí)候會(huì)出現(xiàn)點(diǎn)問題,就是窗體界面會(huì)無法關(guān)閉,要結(jié)束任務(wù)才能結(jié)束進(jìn)程。例如下面代碼

首先按習(xí)慣的方法,設(shè)定線程更新UI

a2.CheckForIllegalCrossThreadCalls = false;  //a2為窗體名稱

下面的代碼就是從數(shù)據(jù)庫里取得數(shù)據(jù)并綁定

復(fù)制代碼 代碼如下:

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("程序發(fā)生錯(cuò)誤,信息: " + 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;  //綁定數(shù)據(jù)           

            }
        }

到這里完成的綁定的工作,運(yùn)行查看一下效果,其實(shí)這樣是會(huì)出現(xiàn)窗體假死的現(xiàn)象。

下面通過Invoke 來實(shí)現(xiàn)

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

然后通過dataBin來綁定DataGridView

復(fù)制代碼 代碼如下:

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

在線程里面調(diào)用下面方法

復(fù)制代碼 代碼如下:

//綁定數(shù)據(jù)
                if (this.InvokeRequired)
                {
                    updateDG ur = new updateDG(dataBin);
                    this.Invoke(ur, dt);
                }

完整的代碼如下:

復(fù)制代碼 代碼如下:

        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("程序發(fā)生錯(cuò)誤,信息: " + 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;//綁定數(shù)據(jù)

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

查運(yùn)行查看一下,你就會(huì)發(fā)現(xiàn)結(jié)果了

相關(guān)文章

最新評(píng)論