0

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.

7
  • You're not making all these calls at once. You're making it sequentially. async != parallelism. Also, you're not really async. You're spinning a thread and than wait on it to finish. Commented Aug 20, 2015 at 20:52
  • Is there a way to adapt this to be parallel? Commented Aug 20, 2015 at 20:53
  • @MarcinJuraszek It's not async, but it is parallel. Commented Aug 20, 2015 at 20:56
  • Each method call within Task.Run simply sets up for and executes a SQL call. Given that, do you think calling them in parallel is a benefit? Or am I just blocking the threads? Commented Aug 20, 2015 at 20:57
  • @oppassum you are definitely just blocking the threads. The benefit is that the entire operation will complete sooner, but it also required more resources. Commented Aug 20, 2015 at 21:03

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.