搜索数据库时 UI 性能缓慢,尽管存在线程
本文关键字:存在 线程 缓慢 数据库 UI 性能 搜索 | 更新日期: 2023-09-27 18:34:46
我目前正在处理我的项目,我已经面临一段时间的问题是,当您在数据库中搜索数据时,性能会降低并且响应迟钝。
我已经创建了一个线程,但它仍然让我头疼
//to start the thread when textbox has change
private void textBox1_TextChanged(object sender, EventArgs e)
{
ThreadStart thread2Start = new ThreadStart(searchMyData);
Thread t2 = new Thread(thread2Start);
t2.Start();
}
public void searchMyData()
{
if (radGridView1.InvokeRequired)
{
radGridView1.Invoke(new Action(() =>
{
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
connection.Open();
try
{
if(!(textBox1.Text=="Search Students"))
{
cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * from studenttable where studname like'" + textBox1.Text + "%' OR studnum like'" + textBox1.Text + "%' OR studcourse like'" + textBox1.Text + "%' OR studemail like'" + textBox1.Text + "%' OR studsec like'" + textBox1.Text + "%' OR studgender like'" + textBox1.Text + "%' ";
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
radGridView1.DataSource = ds.Tables[0].DefaultView;
connection.Close();
}
}
}
}
}
那么,有什么解决方案可以提高我的程序的性能呢?
你在 UI 线程中运行 searchMyData(( 函数,这就是它很慢的原因。尝试将代码更新为以下内容,
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (txtSymbol.Text == "Search Students")
{
ParameterizedThreadStart pts = new ParameterizedThreadStart(searchMyData);
Thread t = new Thread(pts);
t.Start(txtSymbol.Text);
}
}
public void searchMyData(object state)
{
try
{
string text = state.ToString();
using (MySqlConnection connection = new MySqlConnection(MyConnectionString))
{
connection.Open();
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * from studenttable where studname like'" + text + "%' OR studnum like'" + text + "%' OR studcourse like'" + text + "%' OR studemail like'" + text + "%' OR studsec like'" + text + "%' OR studgender like'" + text + "%' ";
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
radGridView1.Invoke(new Action(() => { radGridView1.DataSource = ds }));
}
}
catch(Exception ex) { }
}
没有必要为这种事情引入显式线程。此代码中需要时间的操作是打开数据库连接并填充数据集。您可以轻松地使用任务和异步/等待在后台执行此操作。我不认为 MySql 连接和适配器具有异步方法,因此您可能必须将它们包装在任务中,如下所示:
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (txtSymbol.Text == "Search Students")
{
radGridView1.DataSource = await SearchMyDataAsync(txtSymbol.Text);
}
}
async Task<DataSet> SearchMyDataAsync(object state)
{
try
{
using (MySqlConnection connection = new MySqlConnection(MyConnectionString))
{
await Task.Run(() => connection.Open()); //Or possibly connection.OpenAsync() if it exists...
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * from studenttable where studname like'" + text + "%' OR studnum like'" +
text + "%' OR studcourse like'" + text + "%' OR studemail like'" + text +
"%' OR studsec like'" + text + "%' OR studgender like'" + text + "%' ";
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
await Task.Run(adap.Fill(ds));
return ds;
}
}
catch (Exception ex)
{
throw;
}
}
此外,请注意,无论使用何种技术,从 UI 事件启动异步操作时,都应考虑重入。关于堆栈溢出有一些很好的信息,但基本上,当用户快速连续键入一些字母时,您可能希望避免一次执行多个数据库查询。更多信息在这里: 在异步/等待中重入?