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

编程入门 行业动态 更新时间:2024-10-04 11:21:35
本文介绍了在其他线程中加载数据时显示加载动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have an application running with the database. When I load a tables in the datagridview, my form freezes. How to ensure the smooth load animation during loading tables?

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

I run two threads for animation and load data into the tables, but the animation still does not always work.

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; }

推荐答案

冻结表单时,这意味着UI线程太忙,因此即使您尝试显示正在加载的动画,它也不会进行动画处理.您应该异步加载数据.

When the form is frozen, it means the UI thread is too busy and so even if you try to show a loading animation, it will not animate. You should load data asynchronously.

您可以拥有 async 方法,它返回Task<DataTable>,就像GetDataAsync方法一样,您可以在中看到这篇文章.然后在async事件处理程序中调用它.在事件处理程序中,首先显示正在加载的图像,然后异步加载数据,最后隐藏正在加载的图像.

You can have an async method which returns Task<DataTable> like the GetDataAsync method which you can see in this post. Then call it in an async event handler. In the event handler, first show the loading image, then load data asynchronously and at last hide the loading image.

您可以简单地使用显示gif动画的普通PictureBox作为加载控件.另外,您可能想看看这篇文章,以显示透明的加载图像.

You can simply use a normal PictureBox showing a gif animation as loading control. Also you may want to take a look at this post to show a trasparent loading image.

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(); }

更多推荐

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

本文发布于:2023-11-03 04:40:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   线程   动画   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!