使用Backgroundworker将大量数据加载到ListViewItem

时间:2013-08-09 09:16:08

标签: c# winforms ms-access backgroundworker listviewitem

使用Backgroundworker

将大量数据加载到ListViewItem

当我打开表单时,我无法加载大量数据。所以我所做的是我在我的表单中实现了BackgroundWorker组件,这就是我的代码的样子。

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
     for (int x = 1; x <= 100; x++)
     {
         loadDataByAll(); //Loads All Data to ListView
         backgroundWorker1.RunWorkerAsync();
         backgroundWorker1.ReportProgress(x);
     }
}

对于流程变更事件代码

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

对于loadDataByAll()函数

private void loadDataByAll()
{
     try
     {
         lviewGuard.Items.Clear();
         con.ConnectionString = dbcon.getConnectionString();
         con.Open();
         String query = "SELECT guardid AS a, g_firstname AS b, g_midname AS c, g_lastname AS d, g_age AS e, g_gender AS f, g_address AS g, g_contactno AS h, g_licno AS i, g_lic_until AS j, g_assigned_client AS k, g_schedule_from AS l, g_schedule_to AS m, app_no AS n";
         query += " FROM guards";
         query += " WHERE resigned = '0' ORDER BY app_no DESC";
         OleDbCommand cmd = new OleDbCommand(query, con);
         cmd.ExecuteScalar();
         OleDbDataReader rdr = cmd.ExecuteReader();   
         while (rdr.Read())
         {
             lviewGuard.BeginUpdate();
             ListViewItem lv = new ListViewItem(rdr["a"].ToString());
             lv.SubItems.Add(rdr["b"].ToString());
             lv.SubItems.Add(rdr["c"].ToString());
             lv.SubItems.Add(rdr["d"].ToString());
             lv.SubItems.Add(rdr["e"].ToString());
             lv.SubItems.Add(rdr["f"].ToString());
             lv.SubItems.Add(rdr["g"].ToString());
             lv.SubItems.Add(rdr["h"].ToString());
             lv.SubItems.Add(rdr["i"].ToString());
             lv.SubItems.Add(rdr["j"].ToString());
             lv.SubItems.Add(rdr["k"].ToString());
             lv.SubItems.Add(rdr["l"].ToString());
             lv.SubItems.Add(rdr["m"].ToString());
             lv.SubItems.Add(rdr["n"].ToString());
             lviewGuard.Items.Add(lv);
             lviewGuard.EndUpdate();
          }               
          con.Close();
      }
      catch (Exception)
      {
          MessageBox.Show("MDB Database is not Present", "Microsoft Access Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          Application.ExitThread();
      }
}

最后我的按钮实现

private void btnGo7_Click(object sender, EventArgs e)
{
    for (int x = 1; x <= 100; x++)
    {
         btnGo7.Text = "Running";
         btnGo7.Enabled = false;
    }
    btnGo7.Text = "Go";
    btnGo7.Enabled = true;
    progressBar1.Value = 0;
    tabControl1.Visible = false;
}

很抱歉浪费时间阅读此代码。我真的是一个关于这种多线程和背景工作的完整菜鸟。

1 个答案:

答案 0 :(得分:1)

我建议将数据加载到列表中并将listview放在虚拟模式上。

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode.aspx

为每一行创建listviewitem是没用的。尝试
减少选定的行。 (没有人阅读1 mil listviewitems)。在虚拟模式下设置列表视图..

如果您想坚持自己的版本:

如果loadDataByAll()在线程上运行,请不要触摸listview控件!

我会做这样的事情:(不要把我钉在错字上,没有经过测试)

private void loadDataByAll()
{
 try
 {
     // lviewGuard.Items.Clear();
     con.ConnectionString = dbcon.getConnectionString();
     con.Open();
     String query = "SELECT guardid AS a, g_firstname AS b, g_midname AS c, g_lastname AS d, g_age AS e, g_gender AS f, g_address AS g, g_contactno AS h, g_licno AS i, g_lic_until AS j, g_assigned_client AS k, g_schedule_from AS l, g_schedule_to AS m, app_no AS n";
     query += " FROM guards";
     query += " WHERE resigned = '0' ORDER BY app_no DESC";
     OleDbCommand cmd = new OleDbCommand(query, con);
     cmd.ExecuteScalar();
     OleDbDataReader rdr = cmd.ExecuteReader();   
     List<obj> data = new List<obj>();
     while (rdr.Read())
     {
         data.Add( new obj 
         {
           a = rdr["a"],
           b = rdr["b"]
         });
      }               
      con.Close();


      this.Invoke(new Action(delegate { UpdateListview(data); }));

  }
  catch (Exception)
  {
      MessageBox.Show("MDB Database is not Present", "Microsoft Access Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      Application.ExitThread();
  }

}

public void UpdateListview(List<obj> data)
{
    lviewGuard.BeginUpdate();
    lviewGuard.Items.Clear();
    foreach(obj o in data)
    {
         ListViewItem lv = new ListViewItem(rdr["a"].ToString());
         lv.SubItems.Add(o.b.ToString());
         lv.SubItems.Add(o.c.ToString());

         lviewGuard.Items.Add(lv);
    }

    lviewGuard.EndUpdate();
}