This documentation is archived and is not being maintained.

Emulating breaking in a While activity

This topic applies to Windows Workflow Foundation 4 (WF4).

This sample demonstrates how to break the looping mechanism of the following activities: DoWhile, ForEach, While, and ParallelForEach.

This is useful because Windows Workflow Foundation (WF) does not include any activity to break the execution of these loops.


The sample finds the first reliable vendor from a list of vendors (instances of the Vendor class). Each vendor has an ID, a Name and a numeric reliability value that determines how dependable the vendor is. The sample creates a custom activity called FindReliableVendor that receives two input parameters (a list of vendors and a minimum reliability value) and returns the first vendor of the list that matches the supplied criteria.

Breaking a Loop

Windows Workflow Foundation (WF) does not include an activity to break a loop. The code sample accomplishes breaking a loop by using an If activity and several variables. In the sample, the While activity is broken once the reliableVendor variable is assigned a value other than null.

The following code example demonstrates how the sample breaks a while loop.

// Iterates while the “i” variable is lower than the size of the list 
// and any reliable Vendor is found.      
new While(env => i.Get(env) < this.Vendors.Get(env).Count && reliableVendor.Get(env) == null)
    DisplayName = "Main loop. Breaks when a reliable vendor is found",
    Body = new Sequence
        Activities =
            // This is the if used for setting the value of the break value…
            new If
                DisplayName = "Check for a reliable vendor",

                //  If a vendor satisfies the reliability level…
                Condition = new InArgument<bool>(env => 
                           this.Vendors.Get(env)[i.Get(env)].Reliability > 

                // then assign that vendor to the reliable vendor variable and 
                // the while condition becomes false (exit the loop).
                Then = new Assign<Vendor>
                    To = reliableVendor,
                    Value = new InArgument<Vendor>(env => 

            // Increment the iteration variable. 
            new Assign<int>
                DisplayName = "Increment iteration variable",
                To = i,
                Value = new InArgument<int>(env => i.Get(env) + 1)

To use this sample

  1. Using Visual Studio 2010, open the EmulatingBreakInWhile.sln solution file.

  2. To build the solution, press CTRL+SHIFT+B.

  3. To run the solution, press CTRL+F5.

Dd807393.Important(en-us,VS.100).gif Note:
The samples may already be installed on your machine. Check for the following (default) directory before continuing.


If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.