1

I am developping a MVVM WPF app, and I have some task to do.

  1. first load files csv and parse it
  2. In background don´t block the ui Thread and save the values in the database.To save the rows to the database I need to be with Async Await Task.

My problem is I don´t know how to notice the user with a popup notification or something else that values are already saved in database.

in My ViewModel

private void SaveDatasInDatabase()
{
            ShowLoadingPanel = true;
            _service.SaveValuesInDatabase(rows);
}


 private async void startActions()
{
     await LoadandParseCsv();
     await SaveDatasInDatabase();
}

in my Service.cs

public string SaveDatasInDatabase(List<Object> rows)
{
   Task.Run(async () =>
    {
            await SaveEntity(rows);
            return "Done";
    });
}

Thanks in advance.

Jolynce

jolynice
  • 514
  • 1
  • 8
  • 25

1 Answers1

1

You know that the task has completed once the remainder of the startActions() method is executed:

private async void startActions()
{
    await LoadandParseCsv();
    await SaveDatasInDatabase();

    MessageBox.Show("done");
}

...provided that actually await the SaveEntity method in the SaveDatasInDatabase() method:

public async Task<string> SaveDatasInDatabase(List<Object> rows)
{
    await SaveEntity(rows);
    return "Done";
}

If you just call Task.Run without awaiting the returned Task, you don't know when it has finished.

The return type of the SaveDatasInDatabase method should be Task or Task<T> for you to be able to await it. The same thing applies to the SaveEntity method.

mm8
  • 163,881
  • 10
  • 57
  • 88