显示在其他线程中加载数据期间加载动画

时间:2016-08-25 08:38:38

标签: c# .net multithreading winforms datagridview

我有一个运行数据库的应用程序。当我在datagridview中加载表时,我的表单冻结了。如何在加载表期间确保平滑加载动画?

我为动画运行两个线程并将数据加载到表中,但动画仍然无法正常工作。

 private volatile bool threadRun;

 private void UpdateTab()
 {      
     // Create panel for animation
     Panel loadingPanel = new Panel();              
     // Label, where the text will change
     Label loadingLabel = new Label();
     loadingLabel.Text = "Loading";        

     loadingPanel.Controls.Add(loadingLabel);
     this.Controls.Add(loadingPanel);

     // thread loading animation
     threadRun = true;         

     Task.Factory.StartNew(() =>
     {
         int i = 0;
         string labelText;
         while (threadRun)
         {
             Thread.Sleep(500);
             switch (i)
             {
                 case 0:
                     labelText = "Loading.";
                     i = 1;
                     break;
                 case 1:
                     labelText = "Loading..";
                     i = 2;
                     break;
                 default:
                     labelText = "Loading...";
                     i = 0;
                     break;
            }
            loadingLabel.BeginInvoke(new Action(() => loadingLabel.Text = labelText));
         }
     });

     // thread update DataGridView   
     Thread update = new Thread(ThreadUpdateTab);
     update.Start();
 }

 private void ThreadUpdateTab()
 {
     // SQL Query...
     myDataGridView1.Invoke(new Action(() => myDataGridView1.DataSource = myDataSet1.Tables[0]));
     // ...
     myDataGridView10.Invoke(new Action(() => myDataGridView10.DataSource = myDataSet10.Tables[0]));

     threadRun = false;
 }

1 个答案:

答案 0 :(得分:6)

当表单被冻结时,表示UI线程太忙,因此即使您尝试显示加载动画,也不会设置动画。您应该异步加载数据。

您可以使用async方法返回Task<DataTable>,例如您在this post中可以看到的GetDataAsync方法。然后在async事件处理程序中调用它。在事件处理程序中,首先显示加载图像,然后异步加载数据,最后隐藏加载图像。

您可以简单地使用显示gif动画的普通PictureBox作为加载控件。另外,您可能需要查看this post以显示明显的加载图像。

enter image description here

public async Task<DataTable> GetDataAsync(string command, string connection)
{
    var dt = new DataTable();
    using (var da = new SqlDataAdapter(command, connection))
        await Task.Run(() => { da.Fill(dt); });
    return dt;
}

private async void LoadDataButton_Click(object sender, EventArgs e)
{
    loadingPictureBox.Show();
    loadingPictureBox.Update();
    try
    {
        var command = @"SELECT * FROM Category";
        var connection = @"Your Connection String";
        var data = await GetDataAsync(command, connection);
        dataGridView1.DataSource = data;
    }
    catch (Exception ex)
    {
        //Handle Exception
    }
    loadingPictureBox.hide();
}