JasinCole
Well-known member
- Joined
- Feb 16, 2023
- Messages
- 66
- Programming Experience
- 1-3
I've been trying to wrap my head around this Async/Await pattern and how it is meant to be handled inside a SychronousContext when programming a GUI. There seems to be so many pieces and I think I am just confusing myself trying to research and implement ideas that i've actually hinderd my ability to learn anything meaningful.
As an example of my recent struggles, I am trying to build a dashboard that will make calls to a database using lots of different methods to gather the data and update my viewmodel properties.
What I would like to do is have an overall method that fires off a bunch of other asynchronous methods, when all those methods are finished and complete I want to access the results of those calls and update my viewmodel. For instance... My understanding is this approach will fire off each task and run them Asynchrounously. Then I await each task to completion and assign the result to the property.
That would seem to work, but it's redudant and not very approachable when the amount of information I need is 3 to 4 times that amount or more.
What I'd like to do is
Or even better would be to
How would you do it and why would you use that approach? Is this true Asynchronization? I really like the idea of grouping that task together, `WhenAll()` to get a single state, that way I am not updating the dashboard with some valid and other non-valid date if an exception is thrown.
The next logical step in my brain would be the expand this further. Like so...
Would appreciate a little guidance and some verbose thought process to why and how you would approach the problem.
As an example of my recent struggles, I am trying to build a dashboard that will make calls to a database using lots of different methods to gather the data and update my viewmodel properties.
What I would like to do is have an overall method that fires off a bunch of other asynchronous methods, when all those methods are finished and complete I want to access the results of those calls and update my viewmodel. For instance... My understanding is this approach will fire off each task and run them Asynchrounously. Then I await each task to completion and assign the result to the property.
C#:
public async Task LoadAccountBalancesAsync()
{
var checkingTask = GetAccountAsync(int:<accountnum>);
var savingsTask = GetAccountAsync(int:<accountnum>);
var payrollTask = GetAccountAsync(int:<accountnum>);
//Etc...
Checking = await checkingTask;
Savings = await //...
Payroll = await //...
}
That would seem to work, but it's redudant and not very approachable when the amount of information I need is 3 to 4 times that amount or more.
What I'd like to do is
C#:
public async Task LoadAccountBalancesAsync()
{
var tasks = Task[]
{
GetAccountAsync(int:<accountnum>);
//etc...
}
await Task.WhenAll(task);
//Update properties... But how?
}
Or even better would be to
C#:
public async Task LoadAccountBalances()
{
var tasks = new List<Task>();
foreach (var a in accounts)
{
tasks.Add(GetAccountAsync(a))
}
await Task.WhenAll(tasks);
//But again how do I access the result of the task?
}
How would you do it and why would you use that approach? Is this true Asynchronization? I really like the idea of grouping that task together, `WhenAll()` to get a single state, that way I am not updating the dashboard with some valid and other non-valid date if an exception is thrown.
The next logical step in my brain would be the expand this further. Like so...
C#:
public async Task LoadDashBoard()
{
//Run as Task Asynchronous
var tasks = Task[]
{
LoadAccountBalances();
LoadOutstandingAccountsReceivables();
LoadOutstandingAccountsPayables();
LoadJobOverUnder();
LoadOustandingChangeOrders();
}
await Task.WhenAll(tasks);
}
Would appreciate a little guidance and some verbose thought process to why and how you would approach the problem.