views:

317

answers:

2

I'm using Visual Studio 2010 Beta 2 to get a head start on learning to use WF4. I'm working in the designer to create a xaml file. I've added a ForEach activity, and inside that ForEach activity have a flowchart that does some conditional processing. I want to be able to break out of the ForEach if one of the conditions is true, but can't figure out how to do so. In a C# code behind file that would just be to issue a break; , but in the xaml workflow designer, I don't see any mechanism for breaking from a loop. Any suggestions?

+2  A: 

There is no break equivalent in WF4. So either you need to start adding conditional logic to skip the next loops or throw an exception and catch that outside of the ForEach and continue.

And I agree that neither is a very nice option :-(

Maurice
I'm not quite following you on adding conditional logic to skip the next loops, can you give more info on that?
Russ Clark
Just add a boolean variable "Done" to the ForEach activity and inside the body add an If activity checking for "Done" = false and setting Done to true when you want to stop executing. The remainder of the items will be skipped. In other words, just as you would program it if there was no break statement in C#.
Maurice
A: 

Using a While or DoWhile activity in place of a ForEach may be the best option for the activity's limitations on iteration control. It's a little uglier but works just as you're wanting your iteration to.

The conditional could check a bool, such as Broken, and a counter variable to compare it against the collections length. I'm not sure if this is the best for your scenario or for the collection your looping but seems like a viable option.

jlafay