質問

Lets say I have a collection of objects that I need to process asynchronously.

List<Customer> customers = GetAllCustomers();

customers.ForEach(async (e) => { await e.Process(); });

I assume these will be processed asynchronously without holding the current thread. What I would like to know is if there is a limit on how many customers can be in the collection. What if it is 100, 000. Will it just get queued up in the asynch queue?

** I'm also not sure if this is the right approach. I don't really need to await the result of each customer processing. I just want to process the customer data for all seamlessly. I don't care if each process fails as it will be picked up again until successful. I could use Tasks, but I would rather not create threads leading to possible racing conditions etc. **

役に立ちましたか?

解決

What I would like to know is if there is a limit on how many customers can be in the collection. What if it is 100, 000. Will it just get queued up in the asynch queue?

There is no limit. However, each processing is started one at a time; that is, the Process method (which I assume is asynchronous and should be called ProcessAsync) will be called once for each item, returning a Task<T> for each.

If you do want to (asynchronously) wait for all the tasks to complete, you can use Task.WhenAll as such:

await Task.WhenAll(customers.Select(e => e.Process()));

他のヒント

Tasks are more suited to this type of load, you can set options to control how many threads etc..

 List<Task> tasks = new List<Task>();
 customers.ForEach((e) => { Task task = Task.Run(() =>{ e.Process();  tasks.Add(task);})});

 Task.WaitAll(tasks.ToArray());
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top