Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
154 views

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....
McMurphy's user avatar
  • 1,483
2 votes
1 answer
129 views

I have the following class class Person { public required string Name { get; set; } public required string Lastname { get; set; } public override string ToString() => $"{Name} - {...
user19291301's user avatar
0 votes
0 answers
48 views

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 ...
Eduardo Dorneles's user avatar
9 votes
3 answers
8k views

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 ...
sachinbsatav's user avatar
1 vote
1 answer
81 views

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)?[] ...
Nicolas REY's user avatar
2 votes
1 answer
727 views

Having this handler : public async Task<Result> Handle(MyQuery request, CancellationToken cancellationToken) { var cancellationTokenSource = new CancellationTokenSource(); await ...
user7849697's user avatar
0 votes
0 answers
59 views

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 ...
MaciejPL's user avatar
  • 1,069
2 votes
1 answer
378 views

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 ...
Emperor Eto's user avatar
  • 3,775
3 votes
3 answers
4k views

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 ...
user avatar
1 vote
1 answer
257 views

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 ...
RobC's user avatar
  • 1,415
1 vote
3 answers
449 views

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 ...
Piglet's user avatar
  • 29.3k
0 votes
1 answer
3k views

var tasks = new List<Task<TMDbLib.Objects.Movies.Movie>>(); Parallel.ForEach(page, id => { var movieFromTmdb = _tmdbClient.GetMovieAsync(id); tasks.Add(movieFromTmdb); }); ...
Zoinky's user avatar
  • 5,099
0 votes
1 answer
373 views

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. ...
Mark Grecco's user avatar
0 votes
0 answers
125 views

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 ...
Matteo Pietro Peru's user avatar
1 vote
1 answer
641 views

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....
Siva Sankaran's user avatar
1 vote
1 answer
620 views

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. ...
raysefo's user avatar
  • 483
0 votes
1 answer
382 views

I have recursive function for tree. Can I loop children node with Parallel.ForEachAsync? private async Task<List<ResponseBase<BatchRowData>>> SaveDepartments(DepartmentTree node, ...
Zinger's user avatar
  • 9
-1 votes
2 answers
706 views

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....
Tomas's user avatar
  • 18.2k
4 votes
4 answers
646 views

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 ...
Oliver's user avatar
  • 997
2 votes
1 answer
1k views

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 ...
Theodor Zoulias's user avatar
2 votes
1 answer
1k views

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 = ...
tmaj's user avatar
  • 35.9k
3 votes
2 answers
4k views

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 { ...
Davide Briscese's user avatar
1 vote
2 answers
861 views

.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() { ...
ImminentFate's user avatar
2 votes
0 answers
268 views

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. ...
patawa91's user avatar
0 votes
1 answer
5k views

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 ...
Misiu02's user avatar
  • 21
1 vote
1 answer
861 views

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 ...
Darragh's user avatar
  • 2,676
2 votes
1 answer
3k views

The documentation of the ParallelOptions.MaxDegreeOfParallelism property states that: The MaxDegreeOfParallelism property affects the number of concurrent operations run by Parallel method calls that ...
Theodor Zoulias's user avatar
1 vote
1 answer
3k views

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 ...
MarkusParker's user avatar
  • 1,662
9 votes
1 answer
5k views

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 ....
stymie2's user avatar
  • 151
19 votes
2 answers
8k views

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) ...
Dr. Strangelove's user avatar
4 votes
3 answers
8k views

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 ...
user3838575's user avatar
3 votes
1 answer
2k views

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 ...
Hyperwave's user avatar
  • 133
26 votes
1 answer
60k views

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 ...
Doctor Ford's user avatar
6 votes
2 answers
6k views

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 ...
Vic F's user avatar
  • 1,489
2 votes
3 answers
3k views

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? ...
Smutjes's user avatar
  • 69
9 votes
1 answer
7k views

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....
variable's user avatar
  • 9,886
8 votes
3 answers
1k views

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 ...
Jim Buck's user avatar
  • 2,444
6 votes
1 answer
897 views

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 ...
user183872's user avatar
2 votes
2 answers
3k views

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<...
Dunken's user avatar
  • 8,689