Question: I'm trying to replicate a calculator from my Excel spreadsheet to a webpage.
The first field is an input to natural numbers (without commas), the second is to some currency values (with commas, points, etc) and the third is the total, but it has a defined value (for example, 0,38 cents). Both of the fields must multiply and give a result in the third field.

I tried to create a mask using a mask plugin, but had several errors. Tried to use toLocaleStrings too.

I'm having problems with the masks and the results.
That's the expected result :
[![capture](https://i.stack.imgur.com/tuxl4.jpg)](https://i.stack.imgur.com/tuxl4.jpg)

HTML

```
<form>
<!-- simple number to start the calculation -->
<label>Natural Number</label>
<input type="text" id="base" />
<br>
<!-- currency including the cents -->
<label>Currency value</label>
<input type="text" id="ticket" />
<br>
<!-- If the first field is "1" and the second is "1,00", this field has a value of 0.38 cents. -->
<label>Currency total</label>
<input type="text" id="cost" readonly /></form>

```

JQUERY

```
$(document).ready(function(){
$('#cost').mask('000.000.000.000.000,00', {reverse: true});

$('#base').keyup(calculate);
$('#ticket').keyup(calculate)

function calculate(e) {
$('#cost').val($('#base').val() * $('#ticket').val() * 0.38).trigger('input')
}

```

});

Answer: 
> 
> recently I have seen several SO threads related to Parallel.ForEach mixed with async lambdas, but all proposed answers were some kind of workarounds.
> 
> 
> 

Well, that's because `Parallel` *doesn't work* with `async`. And from a different perspective, why would you want to mix them in the first place? They do *opposite things*. `Parallel` is all about adding threads and `async` is all about giving up threads. If you want to do asynchronous work concurrently, then use `Task.WhenAll`. That's the correct tool for the job; `Parallel` is not.

That said, it sounds like you want to use the wrong tool, so here's how you do it...

> 
> How can I ensure that list will contain all items from all iterations executed withing lambdas in each iteration?
> 
> 
> 

You'll need to have some kind of a signal that some code can block on until the processing is done, e.g., `CountdownEvent` or `Monitor`. On a side note, you'll need to protect access to the non-thread-safe `List<T>` as well.

> 
> How will generally Parallel.ForEach work with async lambdas, if it hit await will it hand over its thread to next iteration?
> 
> 
> 

Since `Parallel` doesn't understand `async` lambdas, when the first `await` yields (returns) to its caller, `Parallel` will assume that interation of the loop is complete.

> 
> I assume ParallelLoopResult IsCompleted field is not proper one, as it will return true when all iterations are executed, no matter if their actual lambda jobs are finished or not?
> 
> 
> 

Correct. As far as `Parallel` knows, it can only "see" the method to the first `await` that returns to its caller. So it doesn't know when the `async` lambda is complete. It also will assume iterations are complete too early, which throws partitioning off.
You don't need `Parallel.For/ForEach` here you just need to *await* a list of tasks. 

**Background**

In short you need to be very careful about *async lambdas*, and if you are passing them to an `Action` or `Func<Task>`

Your problem is because `Parallel.For / ForEach` is not suited for the *async and await pattern* or *IO bound tasks*. They are suited for *cpu bound workloads*. Which means they essentially have `Action` parameters and let's the *task scheduler* create the *tasks* for you 

If you want to run multiple *async* tasks at the same time use `Task.WhenAll` , or a *TPL Dataflow* *Block* (or something similar) which can deal effectively with both *CPU bound* and *IO bound* works loads, or said more directly, they can deal with *tasks* which is what an *async method* is. 

Unless you need to do more inside of your lambda (for which you haven't shown), just use a`Select` and `WhenAll`

```
var tasks = items .Select(LongRunningIoOperationAsync);
var results = await Task.WhenAll(tasks); // here is your list of int

```

If you do, you can still use the await, 

```
var tasks = items.Select(async (item) =>
   {
       var x = await LongRunningIoOperationAsync(item);
       // do other stuff
       return x;
   });

var results = await Task.WhenAll(tasks);

```

***Note*** : If you need the extended functionality of `Parallel.ForEach` (namely the Options to control max concurrency), there are several approach, however RX or DataFlow might be the most succinct