When should I use async/await and when should I use parallel.foreach in C#? Are parallel and async/await serve the same purpose? What are the differences in them?
- 
        See this related question: stackoverflow.com/questions/9898441/…Stephen Cleary– Stephen Cleary2012-12-31 15:43:17 +00:00Commented Dec 31, 2012 at 15:43
- 
        See how to call async operations in parallel stackoverflow.com/questions/11564506/…Michael Freidgeim– Michael Freidgeim2017-12-29 06:35:29 +00:00Commented Dec 29, 2017 at 6:35
- 
        10How is this not constructive?oneManArmin– oneManArmin2018-05-04 12:29:25 +00:00Commented May 4, 2018 at 12:29
1 Answer
async/await is about asynchrony, whereas Parallel.ForEach is about parallelism. They're related concepts, but not the same.
Parallel.ForEach is used when you want to execute the same operation on all the items in a collection, in parallel, blocking the current thread until all operations have completed.
async/await is used when the current operation can't make any more progress until a particular asynchronous operation has completed, but you don't want to block the current thread. This is particularly useful in two situations:
- Writing code which basically kicks off various asynchronous operations, one after the other, on the UI thread, accessing the UI briefly between operations. (It's more general than that, but that's a simple example.) You don't want to block the UI thread, but managing all of those asynchronous operations is a pain otherwise.
- Handling lots of long-running operations at a time - e.g. in a web server. Each individual request may take a long time due to calling into other web service, databases etc - but you don't want to have one thread per request, as threads are a relatively expensive resource.
You can combine parallelism and asynchrony by kicking off a new task which will call Parallel.ForEach, and then awaiting that task. (Just as one example.)