Welcome to Windows Workflow Foundation (WF)
Top Tasks :

For Each Activity

File Details
Downloads: 9362 File Size: 285.5kB
Posted By: jamescon Views: 6219
Date Added: 25 Apr 2006

The ForEach activity takes an IEnumerable as input and upon activation it executes the body of the activity once for each item in the enumerator. If a child activity is added to the ForEach activity, a new instance of the child activity is created and executed in every loop. Each instance of the child activity goes through the entire Initialized -> Executing -> Closed life cycle and is removed from the execution context when the loop completes.

The ForEach activity also provides a custom activity designer that inherits from the SequentialActivityDesigner. It is different from the SequentialActivityDesigner in two ways. First, it draws a looping arrow from the bottom of the designer to the top indicating that the execution would loop around. Second, it restricts that only one child activity can be dropped into the designer thus enforce the single child rule.

There are two projects included in the download. One is the ForEachActivity project which contains the implementation of the ForEach custom activity. The other is the ForEachActivityTestWorkflow project which contains a workflow that uses the ForEach activity and a simple host to run the workflow. The ForEachActivity project must be built once before the activity can show up in the design surface of the workflow.

// This function initializes the Items property. Here we're using an ArrayList
// object as our collection. Any type that implements IEnumerable can be used here.
private void InitializeCollection(object sender, EventArgs e)
{
    List<string> list = new List<string>();
    list.Add("value1");
    list.Add("value2");
    list.Add("value3");
    this.forEach1.Items = list;
}

// This function prints out the current item as the collection is iterated through.
private void OnEachHandler(object sender, EventArgs e)
{
    string s = this.forEach1.CurrentItem as string;
    Console.WriteLine("Current collection item is: " + s);
}

Filed under: , , , ,
Comments
 

Palak Chokshi said:

Thanks for putting up this activity. I have been looking for such an activity. I tried using the ForEach Activity in a Nested Loop. I put one ForEach Activity inside another one and I noticed that if you try this.forEach2.CurrentItem in forEach2_Iterating() method this.forEach2.CurrentItem is null. I have set this.forEach1.Items and this.forEach2.Items to List<CUSTOMOBJECT> and List<INT> respectively. I also tried using a string[] for the second forEach's Items property and an ArrayList too but I still get forEach2.CurrentItem as null. I am going to fiddle around with the source code and see if I can get it to work. Thought I should bring it up. Thanks again
31 Aug 2006 7:19 PM
 

Vadivel Kumar said:

Can you tell me where this activity can be applied ? I am wondering how ForEach Activity can be utilized
1 Sep 2006 6:15 AM
 

Palak Chokshi said:

I played around some more with the code and realized that if I use this.InnerForEach.CurrentItem in InnerForEach_Iterating event handler it will always be null since the OuterForEach activity creates a new instance of ForEach from its EnabledActivities array and executes that and sends a reference to that activity as "sender" when it raises the Iterating event for the child ForEach activity. Hence if you want to use the CurrentItem of the inner ForEach in a nested ForEach activity in the Iterating event handler of the inner ForEach you must use: private void InnerForEach(object sender, EventArgs e) { ForEach inner = sender as ForEach; MyObject obj = (MyObject)inner.CurrentItem; }
1 Sep 2006 1:45 PM

Add Comment

Name (required)
Web Site (optional)
Comment (required)
Add

Copyright © 2006 Microsoft Corporation. All Rights Reserved. | Terms of Use | Privacy Statement | Contact Us