C# 尽管有线程,数据的DataGridView加载的UI性能很差

C# 尽管有线程,数据的DataGridView加载的UI性能很差,c#,multithreading,datagridview,C#,Multithreading,Datagridview,我已经为datagridview创建了一个线程来提高性能 但我仍然可以看到我的项目的滞后和反应迟钝。我只想让这一切顺利进行。 还有一个问题,如果我的datagridview中有大量数据,我应该创建一个加载东西来欺骗用户吗 看看我的代码,我做对了吗?还是我应该做些更好的事 public studentManager() { InitializeComponent(); ThreadStart thread1Start = new ThreadStart(Lo

我已经为datagridview创建了一个线程来提高性能 但我仍然可以看到我的项目的滞后和反应迟钝。我只想让这一切顺利进行。 还有一个问题,如果我的datagridview中有大量数据,我应该创建一个加载东西来欺骗用户吗

看看我的代码,我做对了吗?还是我应该做些更好的事

public studentManager()
    {
        InitializeComponent();
        ThreadStart thread1Start = new ThreadStart(LoadGrid);
        Thread t1 = new Thread(thread1Start);
        t1.Start();
    }



 public void LoadGrid()
    {
        if (radGridView1.InvokeRequired)
            radGridView1.Invoke(new Action(() =>
            {
                //For Buttons stuffs
                add.Image = Properties.Resources.adde;
                add.Enabled = true;
                delete.Image = Properties.Resources.deleted;
                delete.Enabled = false;
                update.Image = Properties.Resources.updated;
                update.Enabled = false;
                errorProvider1.Clear();

                //Datagridview
                MySqlConnection connection = new MySqlConnection(MyConnectionString);
                connection.Open();
                try
                {
                    MySqlCommand cmd = connection.CreateCommand();
                    cmd.CommandText = "Select * from studenttable";
                    MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adap.Fill(ds);
                    radGridView1.DataSource = ds.Tables[0].DefaultView;

                    label8.Text = "Student Information loading completed!";

                    radGridView1.Columns[0].HeaderText = "Student Number";
                    radGridView1.Columns[0].Width = 100;
                    radGridView1.Columns[1].HeaderText = "Name";
                    radGridView1.Columns[1].Width = 300;
                    radGridView1.Columns[2].HeaderText = "Section";
                    radGridView1.Columns[2].Width = 50;
                    radGridView1.Columns[3].HeaderText = "Email";
                    radGridView1.Columns[3].Width = 100;
                    radGridView1.Columns[4].HeaderText = "Course";
                    radGridView1.Columns[4].Width = 50;
                    radGridView1.Columns[5].HeaderText = "Gender";
                    radGridView1.Columns[5].Width = 150;
                    radGridView1.Columns[6].IsVisible = false;
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Clone();
                    }
                }
            }));
        else
        {
            label8.Text = "Data information is loading........";
        }


    }

如果我的问题有问题,请通知我,因为我是stackoverflow的新手。谢谢:D

使用BeginInvoke而不是InvokeNot directly related,但是请注意您正在调用connection。在
finally()块中克隆()而不是
Close
。打开
SqlConnection
可能需要一些时间。此外,线程也不能自动确保更好的性能。在您的例子中,主要工作是从数据库中检索数据。不管你的应用程序线程逻辑如何,它都需要同样的时间来完成。使用Invoke将确保代码在UI线程上执行,这就是为什么你的UI会冻结…而且,您需要确定代码的哪一部分需要花费时间…..是SQL查询还是数据绑定本身..如果是SQL查询,则将SQL查询移动到单独的线程中,一旦您有了调用begininvoke来更新UI的数据..如果是数据绑定,则您可能必须实现分页