WF4.0 beta2 provides the Pick activity to complete the event-based control flow. This activity can have multiple PickBranch branches. Each branch has two parts: Trigger and Action. When a Trigger is triggered, the Activity in the Action is executed. As long as one PickBranch Trigger is triggered for a Pick activity, other PickBranch will not be triggered.
1. For example: there are two branches, and we will end after the user input expires. The work is as follows:
2. The XAML of the workflow is as follows:
<Activity mc: Ignorable = "sap" x: Class = "......> <Sequence sad: XamlDebuggerXmlReader. fileName = "...... \ Sequence1.xaml "sap: VirtualizedContainerService. hintSize = "656,462"> <Sequence. variables> <Variable x: TypeArguments = "x: String" Name = "name"/> </Sequence. variables> <sap: WorkflowViewStateService. viewState> <scg: Dictionary x: TypeArguments = "x: String, x: Object"> <x: Boolean x: Key = "IsExpanded"> True </x: boolean> </scg: Dictionary> </sap: WorkflowViewStateService. viewState> <Pick sap: VirtualizedContainerService. hintSize = "634,338"> <PickBranch sap: VirtualizedContainerService. hintSize = "240,292"> <PickBranch. trigger> <local: ReadString BookmarkName = "[& quot; UserName & quot;]" DisplayName = "read input" sap: VirtualizedContainerService
. HintSize = "210,100" Result = "[name]"/> </PickBranch. trigger> <WriteLine DisplayName = "show welcome information" sap: VirtualizedContainerService. hintSize = "210,100" Text = "[& quot; Hello:
O & quot; & amp; name] "/> </PickBranch> <PickBranch sap: VirtualizedContainerService. hintSize = "240,292"> <PickBranch. trigger> <Delay DisplayName = "set expiration time" Duration = "[System. timeSpan. fromSeconds (5)] "sap: VirtualizedContainerService
. HintSize = "210,100"/> </PickBranch. trigger> <WriteLine DisplayName = "Time Expiration Prompt" sap: VirtualizedContainerService. hintSize = "210,100" Text = "expired"/> </PickBranch> </Pick> </Sequence> </Activity>
3. The workflow is designed in a visualized way. You can also use the code method as follows:
Static Activity CreateWF () {Variable <string> name = new Variable <string> (); // Body Sequence body = new Sequence () {Variables = {name }, activities = {new Pick {Branches = {new PickBranch {Trigger = new ReadString {Result = name, BookmarkName = bookmarkName }, action = new WriteLine {Text = new InArgument <string> (env => "Hello:" + name. get (env) }}, new PickBranch {Trigger = new Delay {Duration = TimeSpan. fromSeconds (5) }, Action = new WriteLine {Text = "Time expired" }}}}; return body ;}
4. We need to prepare an activity for reading input. The Code is as follows:
public sealed class ReadString : NativeActivity<string> { [RequiredArgument] public InArgument<string> BookmarkName { get; set; } protected override bool CanInduceIdle { get { return true; } } protected override void Execute(NativeActivityContext context) { context.CreateBookmark(this.BookmarkName.Get(context), new BookmarkCallback(OnReadComplete)); } void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state) { string input = state as string; context.SetValue(this.Result, input); } }
5. The host Program is as follows:
Static string bookmarkName = "UserName"; public static void Main (string [] args) {ManualResetEvent completedEvent = new ManualResetEvent (false); AutoResetEvent idleEvent = new AutoResetEvent (false ); // WorkflowApplication application = new WorkflowApplication (new Sequence1 (); WorkflowApplication application = new WorkflowApplication (CreateWF (); application. idle + = delegate (WorkflowApplicationIdleEventArgs e) {idleEvent. set () ;}; application. completed + = delegate (WorkflowApplicationCompletedEventArgs e) {completedEvent. set () ;}; application. run (); idleEvent. waitOne (); Console. writeLine ("What is your name (5 seconds)"); string text = Console. readLine ();
Application. ResumeBookmark (bookmarkName, text); completedEvent. WaitOne (); Console. WriteLine ("workflow execution completed"); Console. ReadLine ();}