39 questions
1
vote
1
answer
153
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....
0
votes
0
answers
45
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 ...
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)?[] ...
0
votes
0
answers
58
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 ...
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
3
answers
448
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
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. ...
1
vote
1
answer
639
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....
0
votes
1
answer
379
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,
...
4
votes
4
answers
645
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
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 = ...
1
vote
2
answers
860
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()
{
...
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 ...
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 ...
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 ....
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 ...
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
...
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?
...
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 ...
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<...