c# - Is there any benefit to using Task.Factory.StartNew<Action>() in this ASP.NET .ashx code snippet? -


here's .ashx handler creates series of task objects executed in 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 - seems unnecessary         foreach (var action in list.select(t => t.result))         {             action();         } 

each of methods return action cpu intensive work first method below.

private action addheader(xgraphics graphics, httprequest request, reportparameters reportparameters)     {         dataset ds = new mydata().clientheader(reportparameters);          // function waiting called.         return () =>         {           // cpu intensive operation code here.         };     } 

can please me understand if code useful @ 1) asynchronous or multiple task perspective? 2) code creating multiple threads asp.net threadpool?

when debug app, shows 4 separate tasks on machine before runs foreach loop. each of tasks retrieve dataset , think that's because part of code in each method runs before return () action statement.

so whole purpose of code enable multiple database calls in separate tasks before executing each cpu intensive action method? or code doing asynchronous stuff?

it not best way utilize tasks represent thread pool execution units:

  1. because each such task block 1 thread thread pool, , asp.net uses the same thread pool tasks, reduce amount of concurrent requests factor of five.
  2. utilizing thread pool threads database(i/o) bound code is ineffective, because thread pool threads sit idle, use async code database calls.
  3. but not last problem - foreach (var action in list.select(t => t.result)) block execution till gets result each task sequentially. means returned actions not executed ready execute, executed in same order added list negating potential advantage have been gained had executed them ready.

so, conclusion:

such code starts 4 database calls in least resource-effective way, , squanders 4 misused thread pool threads executing "continuation" on original thread in order doesn't utilize possibility 4 task may execute in arbitrary order.

p.s.: , when need cpu-intensive processing not use factory.startnew - use task.run. not actual error, because same thing, rather a recommendation.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -