Hi @Farshad Valizade , Welcome to Microsoft Q&A,
You can use await Task.Run to start the data retrieval task asynchronously.
Before getting data, set the style and scroll speed of the progressBar.
I use Datatable to demonstrate here. When using it, you need to pay attention to changing Form1_Load to async.
using System.Data;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _9_14_x
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataTable dt;
private async void Form1_Load(object sender, System.EventArgs e)
{
// Create DataTable
dt = new DataTable("MyTable");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Ok", typeof(bool));
//Set the progress bar to scroll style
progressBar1.Style = ProgressBarStyle.Marquee;
//Scroll speed: the smaller, the faster 1-100
progressBar1.MarqueeAnimationSpeed = 1;
//Show progress bar or loading animation
progressBar1.Visible = true;
// Call the GetData method asynchronously
await GetDataAsync();
//Hide the progress bar or load animation
progressBar1.Visible = false;
}
private async Task GetDataAsync()
{
// Use Task.Run to perform time-consuming operations in the background thread
// var data = await Task.Run(() => LineBLL.GetAll());
//Simulate asynchronous loading of data, simulating 2 seconds of loading time
await Task.Delay(2000);
// Clear old data
dt.Rows.Clear();
//Add new data
dt.Rows.Add(1, true);
dt.Rows.Add(2, false);
dt.Rows.Add(3, true);
dt.Rows.Add(4, false);
//After the data is loaded, bind the data to the DataGridView
dataGridView1.DataSource = dt;
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.