[Solved] How to optimize and speed up this operation


List<Customer> customers = GetCustomers("ACT");
Task[] tasks = new Task[MaxNumOfConcurrentSaves];
While(customers.Length > 0)
{
     for(int i = 0; i < MaxNumOfConcurrentTasks; i++){
            tasks[i] = SaveCustomerData(customers[i]);
            customers[i] = null;
     }
     customers = List.FindAll<Customer>(customers, aCust => !(aCust == null));
     Task.AwaitAll(tasks)
}

Ok so here’s whats happening (and you’ll have to perfect it for your uses): while we have customers in the list, for every one of them starting at the begining through to the max number of concurrent tasks, start the task to save and set that customer to null. then, after the for loop is over, find all the customer entries in the list that aren’t null and keep them and await the tasks to complete. if there’s nothing left in the list then the loop is over and the save process is complete. SaveCustomerData would return a task that executes he actual save code.

If any one sees issues with this code please edit or bring it to my attention. I have not tested it in this capacity but it is similar to something I’ve done that works.

Edit:

I have recently found something amazing for this task in .Net 4.5 (maybe 4 too, not sure)

List<Customer> customers = GetCustomers("ACT");
Parallel.ForEach(customers, (currentCustomer) => SaveCustomerData(currentCustomer))

Same thing, Multithreaded in one line.

3

solved How to optimize and speed up this operation