I know there are several posts about this before:
I have not got any of this to work.
In my Winform application, the queries to the database are only accessible through async methods. I need the list myObjectsList to be populated with the result before I call the method:
DisplayInTheDatagridview(myObjectsList).
Now this method is always called with an empty list. If I set a breakpoint in my code and wait the result will be finished, so the database has the actual data I want to retrieve. This is what I have tried so far:
public void ShowRowsInDatagridview(List<MyObjects> listRowsToQueryDB)
{
List<MyObjects> myObjectsList = new List<MyObjects>();
var task = client.GetListFromDBAsync(listRowsToQueryDB);
task.Wait();
var result = task.Result;
myObjectsList = (List<MyObjects>)result;
DisplayInTheDatagridview(myObjectsList);
}
public void ShowRowsInDatagridview(List<MyObjects> listRowsToQueryDB)
{
List<MyObjects> myObjectsList = new List<MyObjects>();
var result = client.GetListFromDBAsync(listRowsToQueryDB).GetAwaiter().GetResult();
myObjectsList = (List<MyObjects>)result;
DisplayInTheDatagridview(myObjectsList);
}
public void ShowRowsInDatagridview(List<MyObjects> listRowsToQueryDB)
{
List<MyObjects> myObjectsList = new List<MyObjects>();
var result = Task.Run(async () => await client.GetListFromDBAsync(listRowsToQueryDB)).Result;
myObjectsList = (List<MyObjects>)result;
DisplayInTheDatagridview(myObjectsList);
}