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?
相关问题
- What are ES7 Async functions? [closed]
- Custom TaskScheduler, SynchronizationContext?
- Load script async?
- C# An asynchronous operation cannot be started at
- aio_write on linux with rtkaio is sometimes long
相关文章
- How do do an async ServiceController.WaitForStatus
- Efficient signaling Tasks for TPL completions on f
- With a Promise, why do browsers return a reject tw
- Asynchronous SHA256 Hashing
- Does aiohttp have ORM?
- Can't Inspect Variables When Debugging .NET As
- How can I read JSON from a StringContent object in
- Implement Async Interface synchronous [duplicate]
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:
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.)