Here's an .ashx handler that creates a series of Task objects which are then executed in a foreach loop.

        List<Task<Action>> list = new List<Task<Action>>();

        list.Add(Task.Factory.StartNew<Action>(() => AddHeader(graphics, request, reportParameters)));
        list.Add(Task.Factory.StartNew<Action>(() => AddSubmitter (graphics, request, reportParameters)));
        list.Add(Task.Factory.StartNew<Action>(() => AddActivity(graphics, request, reportParameters)));
        list.Add(Task.Factory.StartNew<Action>(() => AddCharts(graphics, request, reportParameters)));

        //Task.WaitAll(list.ToArray()); //AY - This seems unnecessary
        foreach (var action in list.Select(t => t.Result))

Each of the methods that return an Action does a CPU intensive work like the first method below.

private Action AddHeader(XGraphics graphics, HttpRequest request, ReportParameters reportParameters)
        DataSet ds = new myData().ClientHeader(reportParameters);

        // This will be a function waiting to be called.
        return () =>
          // Some CPU intensive operation code here.

Can someone please help me understand if this code is useful at all 1) from an asynchronous or multiple task perspective? 2) Is this code even creating multiple threads from the ASP.NET threadpool?

When I debug the app, it shows 4 separate tasks on my machine just before it runs the foreach loop. Each of those tasks retrieve the dataset and I think that's because that part of the code in each method runs before the return () Action statement.

So is the whole purpose of this code to enable multiple database calls in separate tasks before executing each CPU intensive action method? Or is the code really doing asynchronous stuff?

Related posts

Recent Viewed