|
|
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); }
|
|
|