1

In my C# winforms application I am loading the data from the database using Task in the background, below is my code. But after uploading the data I am updating my UI control(s) which is BindingSource. This BindingSource component is connected to the DataGrid which will be updated as well.

what I need to know is that what I am doing is the right way or is there another better way to achieve the same goal.

private async void Form_Load(object sender, EventArgs e)
{
    Task loadDataTask = Task.Factory.StartNew(() =>
    {
       // LoadData(); // loading the data from the database
    });

    await loadDataTask.ConfigureAwait(true);

    FormBindingSource.DataSource = _businessObjecsCollection;
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Shax
  • 4,207
  • 10
  • 46
  • 62

1 Answers1

0

if you can afford it, make method which loads data asynchronous. then you will be able to change Form method as:

private async void Form_Load(object sender, EventArgs e)
{
    await LoadDataAsync();

    FormBindingSource.DataSource = _businessObjecsCollection;
}

maybe even

private async void Form_Load(object sender, EventArgs e)
{        
    FormBindingSource.DataSource = await LoadDataAsync();
}

otherwise current method seems fine

ASh
  • 34,632
  • 9
  • 60
  • 82