C# Task Factory -
hi have code below in loop. in loop because number of times needs run can vary dependent on how many items user has added.
var tasklist = new list<task<ienumerable<myobject>>>(); (int = 0; < numofbatches; i++) { var task = task.factory.startnew(() => mymethod(variablea, variableb)); tasklist.add(task); } //wait tasks complete task.waitall(tasklist.cast<task>().toarray()); return tasklist.selectmany(x => x.result);
is there better way can run these tasks in parallel? thinking parallel each loop because number of iterations of loops isn't fixed don't think can use parallel each
there isn't problem code. if have 10,000 items inputted takes 18 minutes , thinking if run tasks in parallel may return faster. if 10,000 items inputted number of batches 10,000/25 = 400
the actuall code in mymethod calls 3rd party external service return data based on data entered user
processing list in parallel easiest of parallel algorithms there is:
parallelenumerable.range(0, numofbatches) .select(_ => mymethod(variablea, variableb)) .tolist();
it code smell create unbounded numbers of tasks because can lead resource exhaustion , code clumsy.
Comments
Post a Comment