由于 UI 性能,当 Datagridview/Listview 从数据库加载数据时放置加载内容

本文关键字:加载 数据 数据库 性能 UI Datagridview Listview 由于 | 更新日期: 2023-09-27 18:35:53

大家好,我是堆栈溢出的新手,所以问题是当 Listview 仍在从我的数据库中加载数据时,我的 UI 总是冻结,即使我已经有一个新的线程,所以我相信我必须在这里放一个加载的东西。

所以基本上当我的列表视图仍在加载数据时,我想像"0 of 100"这样的加载.gif加载图像就足够了。

检查我已经制作的代码:

public BookManager()
    {
        InitializeComponent();
        ParameterizedThreadStart pts = new ParameterizedThreadStart(LoadBooks);
        Thread t = new Thread(pts);
        t.Start();
    }
 public void LoadBooks(object state)
    {
        MySqlConnection connection = new MySqlConnection(MyConnectionString);
        connection.Open();
        this.Invoke(new Action(() =>
        {
        try
        {
            listView1.Clear();
            ImageList myImageList1 = new ImageList();
            myImageList1.ColorDepth = ColorDepth.Depth32Bit;
            listView1.LargeImageList = myImageList1;
            listView1.LargeImageList.ImageSize = new System.Drawing.Size(80, 80);

            MySqlCommand cmd = connection.CreateCommand();
            cmd.CommandText = "Select * from booktable";
            MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
            DataTable ds = new DataTable();
            adap.Fill(ds);
            for (int i = 0;i<ds.Rows.Count;i++)
            {
                byte[] byteBLOBData = (byte[])ds.Rows[i]["bookphoto"];
                var stream = new MemoryStream(byteBLOBData);
                Image bookimages = Image.FromStream(stream);
                myImageList1.Images.Add(bookimages);
                    ListViewItem lsvparent = new ListViewItem();
                    lsvparent.Text = ds.Rows[i]["booktitle"].ToString();
                    listView1.Items.Add(lsvparent.Text, i);
            }
           catch (Exception)
        {
            throw;
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }
        }));
    }

由于 UI 性能,当 Datagridview/Listview 从数据库加载数据时放置加载内容

正如我之前评论的那样,Invoke 方法在 UI 线程上执行代码。所以你创建的线程并没有真正使用:大部分工作都是在UI线程上完成的。

我建议你尝试这样的事情:

public BookManager()
{
    InitializeComponent();
    ParameterizedThreadStart pts = new ParameterizedThreadStart(LoadBooks);
    Thread t = new Thread(pts);
    t.Start();
}
public void LoadBooks(object state)
{
    ImageList myImageList1 = null;
    Invoke(new Action(() =>
    {
        listView1.Clear();
        myImageList1 = new ImageList();
        myImageList1.ColorDepth = ColorDepth.Depth32Bit;
        listView1.LargeImageList = myImageList1;
        listView1.LargeImageList.ImageSize = new System.Drawing.Size(80, 80);
    }));
    MySqlConnection connection = null;
    try
    {
        connection = new MySqlConnection(MyConnectionString);
        connection.Open();
        MySqlCommand cmd = connection.CreateCommand();
        cmd.CommandText = "Select * from booktable";
        MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        adap.Fill(ds);
        for (int i = 0; i < ds.Rows.Count; i++)
        {
            byte[] byteBLOBData = (byte[])ds.Rows[i]["bookphoto"];
            var stream = new MemoryStream(byteBLOBData);
            Image bookimages = Image.FromStream(stream);
            Invoke(new Action(() =>
            {
                myImageList1.Images.Add(bookimages);
                ListViewItem lsvparent = new ListViewItem();
                lsvparent.Text = ds.Rows[i]["booktitle"].ToString();
                listView1.Items.Add(lsvparent.Text, i);
            }));
        }
    }
    finally
    {
        if (connection != null && connection.State == ConnectionState.Open)
        {
            connection.Close();
        }
    }
}