Reputation: 1765
Here's my setup:
public class Workflow
{
List<Detail> Details;
List<Notification> Notifications;
List<Machine> Machine;
public Workflow GetWorkflowInformation(int workflowID)
{
Task<Workflow> workflowTask = fillInWorkflowInfo(workflowID);
Task<List<Detail>> detailsTask = getDetails(workflowID);
Task<List<Notify>> notifyTask = getNotifications(workflowID);
Task<Machine> machineTask = getMachine();
Workflow workflow = workflowTask.Result;
workflow.Details = detailsTask.Result;
workflow.Notifications = notifyTask.Result;
workflow.Machine = machineTask.Result;
return workflow;
}
private async Task<List<Detail>> getDetails(int workflowID)
{
List<Detail> details = new List<Detail>();
await Task.Run( () = >
{
details = Detail.GetDetailsForWorkflow(workflowID);
}
}
private async Task<List<Notify>> getNotifications(int workflowID)
{
List<Notify> notifications = new List<Notify>();
await Task.Run( () = >
{
notifications = Notify.GetNotificationsForWorkflow(workflowID);
}
return notifications;
}
private async Task<Machine> getMachine(int workflowID)
{
Machine machine = new Machine();
await Task.Run( () = >
{
Machine.GetMachineForWorkflow(workflowID);
}
return machine;
}
private async Task<Workflow> fillInWorkflowInfo(int workflowID)
{
Workflow workflow = new Workflow();
await Task.Run( () = >
{
workflow = GetWorkflowInformation(workflowID);
}
return workflow;
}
}
Each one of the methods above correspond to a SQL server call. I'm wondering if I gain anything by doing multiple async/await calls (since my SQL Server can handle multiple calls at once), or if this is just slowing me down because of overheard, or if I'm not implementing something right.
I'm fairly new to async/await, so I'm looking to see if this would be an appropriate implementation, or if I'm missing something completely.
Upvotes: 0
Views: 72
Reputation: 116548
What you are doing isn't truly asynchronous. You are just offloading the work to different threads that are blocked when the query is executing.
Executing multiple queries in parallel can make a single flow complete quicker but it isn't any more efficient than executing these queries one after the other.
To actually reduce the resources (i.e. threads) needed the operations themselves need to be asynchronous (not just offloaded with Task.Run
). That requires support from the client library that you use.
Upvotes: 4