I have a form containing 4 gridviews. I would like to fill these gridviews with objects I query thanks to entity Framework. The problem is that the UI gets stuck while they load. I thus tried to make the query asynchronous but it isn't working. I have tried to do it several ways but my current solution looks like this
private async void SwiftCheck_Load(object sender, EventArgs e)
{
radGridViewLast7Days.DataSource = await MonitoringToolCore.SwiftQueries.ReturnLast7DaysAsync();
radGridViewAmount.DataSource = await MonitoringToolCore.SwiftQueries.CompareAmountsAsync();
radGridViewAmountBySwift.DataSource = await MonitoringToolCore.SwiftQueries.CompareAmountsByTypeAsync();
radGridViewAmountBySender.DataSource = await MonitoringToolCore.SwiftQueries.CompareAmountsBySenderAsync();
//Code to adapt the gridviews layout after data has been bound
this.radGridViewAmountBySender.BestFitColumns();
}
I bind the data in the load event because from my understanding I cannot use await in the constructor. I then use await on each task because Entity Framework isn't thread safe. So if I understood properly, I can't launch each task at the same time and then await them all ?
This is one of the codes that returns the list of objects
public static async Task<List<MsMessage>> ReturnLast7DaysAsync()
{
DateTime currentDate = DateTime.UtcNow.Date.AddDays(-7);
using (var db = new SveulumeContext())
{
var query = db.MsMessages
.Where(u => u.Created >= currentDate);
return await query.ToListAsync();
}
}
The code works and binds the gridViews properly but the UI still blocks during the whole process. What am I doing wrong ?