I wanted to create a workflow that executes several custom activities in parallel and adds the results from each to a shared collection. This is easily accomplished by using WF4’s AddToCollection activity.
The XAML below demonstrates the use of both the Parallel and AddToCollection activities to accomplish this task. I’m also using of the ForEach activity to iterate through my collection to display the results.
<Activity xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"> <Sequence> <Sequence.Variables> <Variable x:TypeArguments="scg:List(x:String)" Default="[New List(Of String)]" Name="MyList" /> </Sequence.Variables> <Parallel> <AddToCollection x:TypeArguments="x:String" Collection="[MyList]" Item="item1" /> <AddToCollection x:TypeArguments="x:String" Collection="[MyList]" Item="item2" /> <AddToCollection x:TypeArguments="x:String" Collection="[MyList]" Item="item3" /> </Parallel> <ForEach x:TypeArguments="x:String" Values="[MyList]"> <ActivityAction x:TypeArguments="x:String"> <ActivityAction.Argument> <DelegateInArgument x:TypeArguments="x:String" Name="item" /> </ActivityAction.Argument> <WriteLine Text="[item]" /> </ActivityAction> </ForEach> </Sequence> </Activity>
Here’s the output produced by running this workflow:
Some notes about this example:
- The XAML was written without the use of Visual Studio’s designer
- There is a collection variable of type List<string> declared named “MyList”; this requires us to import the namespace System.Collections.Generic
- The AddToCollection activities’ Collection properties are bound to MyList
- The ForEach activity’s Values property is bound bound to MyList
- The ForEach activity creates a delegate argument named “item” which is then passed in to the ForEach activity’s WriteLine activity