Skip to main content
Rollback to Revision 2
Source Link
Theodor Zoulias
  • 46.1k
  • 8
  • 112
  • 155

I have seen Parallel.ForEach blocks the calling thread until the specified actions complete. However, it does not automatically await async method callsused inappropriately, similar to how normally callingand I figured an async method without awaiting itexample in this question would continue without waiting for the async method to completehelp.

An example ofWhen you run the code below in a Console app, you will see how the tasks executed in Parallel.ForEach not automatically awaiting an async method call:doesn't block the calling thread. This could be okay if you don't care about the result (positive or negative) but if you do need the result, you should make sure to use Task.WhenAll.

Using the Parallel.ForEach doeswith a Task will not automatically await async actionsblock the calling thread. If you need to wait for async actions to completecare about the result, make sure to await themthe tasks.

Parallel.ForEach blocks the calling thread until the specified actions complete. However, it does not automatically await async method calls, similar to how normally calling an async method without awaiting it would continue without waiting for the async method to complete.

An example of Parallel.ForEach not automatically awaiting an async method call:

Parallel.ForEach does not automatically await async actions. If you need to wait for async actions to complete, make sure to await them.

I have seen Parallel.ForEach used inappropriately, and I figured an example in this question would help.

When you run the code below in a Console app, you will see how the tasks executed in Parallel.ForEach doesn't block the calling thread. This could be okay if you don't care about the result (positive or negative) but if you do need the result, you should make sure to use Task.WhenAll.

Using the Parallel.ForEach with a Task will not block the calling thread. If you care about the result, make sure to await the tasks.

Corrected that Parallel.ForEach does wait for its actions to complete, but acts like most other code that does not await async action.
Source Link

I have seen Parallel.ForEach used inappropriatelyblocks the calling thread until the specified actions complete. However, and I figuredit does not automatically await async method calls, similar to how normally calling an example in this questionasync method without awaiting it would helpcontinue without waiting for the async method to complete.

When you run the code below in a Console app, you will see how the tasks executed inAn example of Parallel.ForEach doesn't block the calling thread. This could be okay if you don't care about the result (positive or negative) but if you do need the result, you should make sure to use Task.WhenAll.not automatically awaiting an async method call:

Using the Parallel.ForEach with a Task willdoes not block the calling threadautomatically await async actions. If you care about the resultneed to wait for async actions to complete, make sure to await the tasksthem.

I have seen Parallel.ForEach used inappropriately, and I figured an example in this question would help.

When you run the code below in a Console app, you will see how the tasks executed in Parallel.ForEach doesn't block the calling thread. This could be okay if you don't care about the result (positive or negative) but if you do need the result, you should make sure to use Task.WhenAll.

Using the Parallel.ForEach with a Task will not block the calling thread. If you care about the result, make sure to await the tasks.

Parallel.ForEach blocks the calling thread until the specified actions complete. However, it does not automatically await async method calls, similar to how normally calling an async method without awaiting it would continue without waiting for the async method to complete.

An example of Parallel.ForEach not automatically awaiting an async method call:

Parallel.ForEach does not automatically await async actions. If you need to wait for async actions to complete, make sure to await them.

Removed fluff
Source Link
Nimantha
  • 6.5k
  • 6
  • 32
  • 78

Using the Parallel.ForEach with a Task will not block the calling thread. IfIf you care about the result, make sure to await the tasks.

~Cheers

Using the Parallel.ForEach with a Task will not block the calling thread. If you care about the result, make sure to await the tasks.

~Cheers

Using the Parallel.ForEach with a Task will not block the calling thread. If you care about the result, make sure to await the tasks.

Source Link
Rogala
  • 2.8k
  • 28
  • 27
Loading