ParallelForEach Activity Designer

Applies to: yesVisual Studio noVisual Studio for Mac noVisual Studio Code

The ParallelForEach<T> activity enumerates the elements of a collection and executes an embedded statement for each element of the collection in parallel, which is asynchronously on the same thread. Use this flow control activity instead of the Sequence activity if the child activities of this activity are expected to go idle.

The ParallelForEach<T> activity has a CompletionCondition property that contains a user specified Visual Basic expression. The ParallelForEach<T> activity evaluates this property after each branch completes. If it evaluates to true, then the ParallelForEach<T> activity completes without executing the other branches. If the CompletionCondition does not evaluate to true, then the ParallelForEach<T> activity completes when all of its child activities have completed.

The ParallelForEach<T> Activity

ParallelForEach<T> enumerates its values and schedules the Body for every value it enumerates on. It only schedules the Body. How the body executes depends on whether the Body goes idle.

If the Body does not go idle, it executes in a reverse order because the scheduled activities are handled as a stack, the last scheduled activity executes first. For example, if you have a collection of {1,2,3,4}in ParallelForEach<T> and use a WriteLine as the body to write the value out. You have 4, 3, 2, 1 printed out in the console. This is because WriteLine does not go idle so after 4 WriteLine activities got scheduled, they executed using a stack behavior (first in last out).

But if you have activities in the Body that can go idle, like a Receive activity or Delay activity. Then there is no need to wait for them to complete. ParallelForEach<T> goes to the next scheduled body activity and try to execute it. If that activity goes idle too, ParallelForEach<T> moves on again the next body activity.

Using the ParallelForEach<T> Activity Designer

Access the ParallelForEach<T> activity designer in the Control Flow category of the Toolbox.

The ParallelForEach<T> activity designer can be dragged from the Toolbox and dropped on to the Workflow Designer surface wherever activity designers are normally placed, for example, inside of a Sequence activity designer. After dropping it into the Workflow Designer, it creates a ParallelForEach<T> activity, which by default contains a DisplayName of ParallelForEach<Int32>.

ParallelForEach<T> Properties in the Workflow Designer

The following table shows the most useful ParallelForEach<T> activity properties and describes how they are used in the designer.

Property Name Required Usage
DisplayName False Specifies the friendly display name of the activity designer in the header. The default value is ParallelForEach<Int32>. The value can be optionally edited in the Properties grid or directly on the activity designer header.
Body False The activity to execute for each item in the collection. To add the Body activity, drop an activity from the toolbox into the Body box on the ParallelForEach<T> activity designer with hint text "Drop Activity Here".
TypeArgument True The type of the items in the Values collection specified by the generic parameter T. By default, TypeArgument is set to Int32. To change the type T in the ParallelForEach<T> activity designer, change the value of the TypeArgument combo box in the Property Grid.
Values True The collection of items to iterate over. To set the Values, type a Visual Basic expression in the Values box on the ForEach<T> activity designer in the box with the hint text "Enter a VB expression" or in Values box on the Properties window.
CompletionCondition Evaluated after each iteration completes. If it evaluates to true, then the scheduled pending iterations are canceled. If this property is not set, all scheduled statements execute until completion.

By default, the loop iterator is named item. You can change the name of the iterator variable in the ForEach box in ParallelForEach<T> activity designer. The loop iterator can be used in expressions in the children of the ParallelForEach<T> activity.

See also