加载循环延迟c#
本文关键字:延迟 循环 加载 | 更新日期: 2023-09-27 18:03:17
我有一个将数据加载到数据网格的应用程序。然而,加载过程需要几秒钟。我想在数据加载时显示一个加载圆,但是直到数据完成加载后才显示这个圆。代码是:
public void processbutton(object sender, EventArgs e)
{
pictureBox1.Visible = true;
Image image=Image.FromFile("C:''Users''me''Documents''Curr''Projects''Proj''Proj''load.gif");
pictureBox1.Image = image;
//--------------------------------------------------------------------------
string ssqlconnectionstring = "string";
SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
string comm = "SELECT * from Table";
sqlconn.Open();
//---------------------REST OF CODE-------------------------
必须在单独的线程中执行所有长时间运行的任务,否则会阻塞UI线程。
查找BackgroundWorker
类
或者您可以使用Task类,像这样:
public async void processbutton(object sender, EventArgs e)
{
pictureBox1.Visible = true;
Image image=Image.FromFile("C:''Users''me''Documents''Curr''Projects''Proj''Proj''load.gif");
pictureBox1.Image = image;
//--------------------------------------------------------------------------
await Task.Run(() =>
{
string ssqlconnectionstring = "string";
SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
string comm = "SELECT * from Table";
sqlconn.Open();
//---------------------REST OF CODE-------------------------
});