How to correctly write Parallel.For with async methods

Filling The Stack is What I DO

How would I structure the code below so that the async method gets invoked?

Parallel.For(0, elevations.Count(), delegate(int i)
{
   allSheets.AddRange(await BuildSheetsAsync(userID, elevations[i], includeLabels));
});
svick

Parallel.For() doesn't work well with async methods. If you don't need to limit the degree of parallelism (i.e. you're okay with all of the tasks executing at the same time), you can simply start all the Tasks and then wait for them to complete:

var tasks = Enumerable.Range(0, elevations.Count())
    .Select(i => BuildSheetsAsync(userID, elevations[i], includeLabels));
List<Bitmap> allSheets = (await Task.WhenAll(tasks)).SelectMany(x => x).ToList();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to correctly write async method?

From Dev

How to correctly write async method?

From Dev

Running async methods in parallel

From Dev

Write async method correctly

From Dev

Write async method correctly

From Dev

How to get C# async methods to actually run in parallel?

From Dev

Promises: How to execute async methods in parallel then execute method

From Dev

async.js parallel not working correctly

From Dev

how to write a parallel array

From Dev

how to handle async actions correctly?

From Dev

How to use @Async correctly in Spring

From Dev

how to handle async actions correctly?

From Dev

Parallel.Invoke does not wait for async methods to complete

From Dev

Java 8 - Call methods async in parallel and combine their results

From Dev

Parallel.Invoke does not wait for async methods to complete

From Dev

How to correctly inherit thread culture in a parallel block?

From Dev

How to use QProcess write correctly?

From Dev

How to write a sql trigger correctly

From Dev

How to make Repository Methods Async?

From Dev

How to implement truly async methods?

From Dev

is asynchronous version of relaycommand required in order to run async methods correctly

From Dev

async but not parallel

From Dev

how to make a for loop work with a async.parallel()

From Dev

How to execute functions in parallel with async.js?

From Dev

How to verify that n methods are executing in parallel?

From Dev

How to use Async.Parallel within an async workflow?

From Dev

How to write multicore sorting using GNU Parallel

From Dev

How do i write a java function in parallel?

From Dev

How to write Parallel ForEach in Viola-Jones

Related Related

HotTag

Archive