39 questions
1
vote
1
answer
154
views
Why is CancellationToken.None generating CS0426 The type name 'CancellationToken' does not exist in the type 'None'
Why does this code result in a compiler error:
await Parallel.ForEachAsync(files, loopOptions, async (file, CancellationToken.None) =>
but this code snippet does not?
var x = CancellationToken....
2
votes
1
answer
129
views
Parallel.ForEachAsync does not change properties of IEnumerable items [duplicate]
I have the following class
class Person
{
public required string Name { get; set; }
public required string Lastname { get; set; }
public override string ToString() => $"{Name} - {...
0
votes
0
answers
48
views
SSRS report files creation within a Parallel.ForEachAsync don't have filenames matching their content on C# .NET 8. Could someone explain the cause?
In order to obtain a faster file generation from SSRS, I've created a Parallel.ForEachAsync code to split among 4 threads then: do some database queries, followed by SSRS requests to generate reports ...
9
votes
3
answers
8k
views
Task.WhenAll vs Parallel.ForEachAsync - Which approach is best and why?
I am trying to gain understanding of Threading and Task Parallel Library in .NET. So I was experimenting with running tasks concurrently using 2 approaches as can be seen below.
Some background: I ...
1
vote
1
answer
81
views
Parallel with Zip lists in C#
I have a code in async that looks like this:
List<Task<(decimal, decimal)?>> tasks = offsets
.Zip(prices, async (offset, price) => await Func(offset, price));
(decimal, decimal)?[] ...
2
votes
1
answer
727
views
How to use CancellationTokenSource in Parallel.ForEachAsync
Having this handler :
public async Task<Result> Handle(MyQuery request, CancellationToken cancellationToken)
{
var cancellationTokenSource = new CancellationTokenSource();
await ...
0
votes
0
answers
59
views
Web App, control number of available tasks in Hangifre jobs, each executing Parallel.ForEachAsync
I'm building an app that will schedule some jobs with Hangfire. They will from time to time execute at the same time. Each jobs has number of "configs" to process. To speed up things it is ...
2
votes
1
answer
378
views
HttpClient and Parallel.ForEachAsync for large number of file transfers
I have a need for one ASP.NET Core server to download via the backend a massive number of files from another server using GET requests. (The platform doesn't matter, assume Dropbox, OneDrive, or ...
3
votes
3
answers
4k
views
Parallel.ForAsync in C# .NET?
I want to execute a for loop in C# in parallel asynchronously. But the Parallel class contains only a synchronous For method and an asynchronous ForEachAsync method (.NET 6). This looks like an ...
1
vote
1
answer
257
views
Is calling interface method from within Parallel.ForEachAsync() thread safe?
Here's some code just to hopefully make clear the situation I'm talking about:
public class Processor
{
private readonly IRepository _repo;
private readonly IApiSrevice _apiService
...
1
vote
3
answers
449
views
Do I need a mutex when Parallel.ForeachAsync?
From my understanding the delegate in a Parallel.ForeachAsync call is executed multiple times concurrently.
What if that delegate operates on a variable that is not local to the delegate?
Say I ...
0
votes
1
answer
3k
views
Which is more efficient Parallell.ForEach or Parallel.ForEachAsync in .NET 8.0 preview
var tasks = new List<Task<TMDbLib.Objects.Movies.Movie>>();
Parallel.ForEach(page, id =>
{
var movieFromTmdb = _tmdbClient.GetMovieAsync(id);
tasks.Add(movieFromTmdb);
});
...
0
votes
1
answer
373
views
How to invoke an event within Parallel.ForEachAsync in C# without hanging UI
I'm trying to invoke an event within a Parallel.ForEachAsync, and it's hanging my UI or erroring back to the program.cs
The event on my class is being consumed by the UI to increment a progress bar. ...
0
votes
0
answers
125
views
How to determine the number of parallelism degrees in a ParallelForeachAsync combined with DbContext
after reading some docs on how to manage DB connections via DbContext (that isn't thread safe) inside a Parallel.ForEach, I was wondering if there was an intelligent way to manage the ...
1
vote
1
answer
641
views
How to return inside a ValueTask returning lambda of Parallel.ForEachAsync?
In general we can await an async method invocation in any one of the Branch of execution. And other branches we can simply return. This will not give any warning.
But when we do the same in Parallel....
1
vote
1
answer
620
views
How to handle Parallel.ForEachAsync completed tasks if there is an error?
I am working on a worker service. In this worker service, I am calling a web API of mine that calls an external API. My web API is an asp.net core web API saves requests and responses into database. ...
0
votes
1
answer
382
views
await Parallel.ForEachAsync in recursive function
I have recursive function for tree.
Can I loop children node with Parallel.ForEachAsync?
private async Task<List<ResponseBase<BatchRowData>>> SaveDepartments(DepartmentTree node,
...
-1
votes
2
answers
706
views
Parallel.ForEachAsync keep sort order
I am trying to execute file upload using Parallel.ForEachAsync, it works but loses the sort order. Is there any method to synchronize sort order or source and destination lists?
await Parallel....
4
votes
4
answers
646
views
From IEnumerable<Task<T>> to IAsyncEnumerable<T> by yield returning inside a Parallel.ForEach/Parallel.ForEachAsync gives error CS1621
In a .NET 6 project, I have to call a web API which is offset paginated (page/per page) and I would like to make the n calls parallel as far as possible.
This is the method which calls the API one ...
2
votes
1
answer
1k
views
How to run a Parallel.ForEachAsync loop with NoBuffering?
The synchronous Parallel.ForEach method has many overloads, and some of them allow to configure the parallel loop with the EnumerablePartitionerOptions.NoBuffering option:
Create a partitioner that ...
2
votes
1
answer
1k
views
Parallel.ForEachAsync Task vs ValueTask
I'm trying out Parallel.ForEachAsync and the compiler is kind enough to inform me that the body is a func that returns a ValueTask, not a Task.
Stopwatch sw = Stopwatch.StartNew();
var numbers = ...
3
votes
2
answers
4k
views
Actual maximum concurrent tasks of Parallel.ForEachAsync
I would expect this code to take 1 second to execute:
public async void Test()
{
DateTime start = DateTime.Now;
await Parallel.ForEachAsync(new int[1000], new ParallelOptions { ...
1
vote
2
answers
861
views
How do I use Parallel.ForEachAsync in VB.NET using .NET 6?
.NET 6 introduced the Parallel.ForEachAsync method which works pretty well in C#, but I'm running into issues using it in VB.NET.
Namely, the following example in C#:
using HttpClient client = new()
{
...
2
votes
0
answers
268
views
Why Does Parallel.ForEachAsync Use All Threads On My Box Waiting Using AsyncManualResetEvent?
What I'm trying to accomplish is to get a particular record before I process the rest.
Unfortunately, I don't have the freedom to enumerate twice to get the 1 record I need before processing the rest. ...
0
votes
1
answer
5k
views
Parallel.ForEachAsync help needed
Ok. So I am going to start by apologizing up front as I know there have been a lot of question asked about Parallel and Async. However, even after searching I can not wrap my brain around how this ...
1
vote
1
answer
861
views
How do I determine an appropriate value for MaxDegreeOfParallelism when using Parallel.ForEachAsync
The example Scott Hanselman gives on his blog for using Parallel.ForEachAsync in .NET 6 specifies the value of MaxDegreeOfParallelism as 3.
However, if unspecified, the default MaxDegreeOfParallelism ...
2
votes
1
answer
3k
views
What is the meaning of the MaxDegreeOfParallelism = -1 in Parallel operations in .NET 6?
The documentation of the ParallelOptions.MaxDegreeOfParallelism property states that:
The MaxDegreeOfParallelism property affects the number of concurrent operations run by Parallel method calls that ...
1
vote
1
answer
3k
views
Can .NET 6's Parallel.ForEachAsync be nested?
I want to do calculations with an outer and an inner loop which I can do in parallel.
Furthermore, I want to use the async/await-based programming model. In the outer loop there is a place where a ...
9
votes
1
answer
5k
views
How to break the Parallel.ForEachAsync loop, not cancel it?
In .NET 5 we had Parallel.ForEach which you were able to use ParallelLoopState.Break() method to stop additional iterations from processing. Allowing current ones to complete processing.
But the new ....
19
votes
2
answers
8k
views
Stop Parallel.ForEachAsync
In C#, I am interested in stopping a Parallel.ForEachAsync loop (considering the differences between Stop and Break); for Parallel.ForEach I can do the following:
Parallel.ForEach(items, (item, state) ...
4
votes
3
answers
8k
views
Parallel.ForEachAsync is not waiting for all tasks
Below is sample console app and output is
Output is different each time and is fine but it needs to complete all tasks before I print result.
It seems that Parallel.ForEachAsync is not waiting for ...
3
votes
1
answer
2k
views
Is it possible to throttle Parallel.ForEachAsync in .NET 6.0 to avoid rate limiting?
I'm fairly new to programming (< 3 years exp), so I don't have a great understanding of the subjects in this post. Please bear with me.
My team is developing an integration with a third party ...
26
votes
1
answer
60k
views
Using Parallel.ForEachAsync
I'm trying to run a Parallel.ForEachAsync(), but I am getting these two errors:
Error 1:
Argument 2: can not convert from System.Threading.Tasks.ParallelOptions to System.Threading.CancellationToken
...
6
votes
2
answers
6k
views
The need for two cancellation tokens in .NET 6 Parallel.ForEachAsync?
I was experimenting with how to break out of a ForEachAsync loop. break doesn't work, but I can call Cancel on the CancellationTokenSource. The signature for ForEachAsync has two tokens - one as a ...
2
votes
3
answers
3k
views
Trying to download huge amount of files more efficiently
I'm trying to download approx. 45.000 image files from an API.
The image files have less than 50kb each.
With my code this will take 2-3 Hours.
Is there an more efficient way in C# to download them?
...
9
votes
1
answer
7k
views
Is Parallel.ForEachAsync a replacement to a plain for loop + append to task list (async await Task.Run)+ WhenAll?
Say I want to make parallel API post requests.
In a for loop I can append the http post call into a list of tasks, (each task invoked using Task.Run) and then wait for all to finish using await Task....
8
votes
3
answers
1k
views
Factors for determining the degree of parallelism for the ForEachAsync
Below is an implementation of ForEachAsync written by Stephen Toub.
public static Task ForEachAsync<T>(this IEnumerable<T> source, int dop,
Func<T, Task> body)
{
return ...
6
votes
1
answer
897
views
What do I specify as the Dop parameter for ForEachAsync extension method?
I recently discovered the following code below to effectively run lots of I/O bound tasks:
Implementing a simple ForEachAsync, part 2
I'm under the impression the following are true:
This is much ...
2
votes
2
answers
3k
views
ForEachAsync with Result
I'm trying to change Stephen Toub's ForEachAsync<T> extension method into an extension which returns a result...
Stephen's extension:
public static Task ForEachAsync<T>(this IEnumerable<...