WF4.0 beta2提供了Pick活动用于完成基于事件的控制流。 该活动可以有多个PickBranch分支。每个分支有Trigger和Action两部分。当Trigger 被触发时,会执行Action中的Activity。Pick活动只要有一个PickBranch的Trigger被触发,其他PickBranch就不会被触发了 。
1.举例说明:有两个分支,我们等待用户输入过期就结束,工作如下图:
2.工作流对应的XAML如下:
<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="["UserName"]" DisplayName="读取输入" sap:VirtualizedContainerService
.HintSize="210,100" Result="[name]" /> </PickBranch.Trigger> <WriteLine DisplayName="显示欢迎信息" sap:VirtualizedContainerService.HintSize="210,100" Text="["你好:
o " & name]" /> </PickBranch> <PickBranch sap:VirtualizedContainerService.HintSize="240,292"> <PickBranch.Trigger> <Delay DisplayName="设置过期时间" Duration="[System.TimeSpan.FromSeconds(5)]" sap:VirtualizedContainerService
.HintSize="210,100" /> </PickBranch.Trigger> <WriteLine DisplayName="时间过期提示" sap:VirtualizedContainerService.HintSize="210,100" Text="时间过期" /> </PickBranch> </Pick> </Sequence> </Activity>
3.上面是可视化的方式设计工作流,还可以使用代码方式,如下:
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 => "你好:" + name.Get(env)) } }, new PickBranch { Trigger = new Delay { Duration = TimeSpan.FromSeconds(5) }, Action = new WriteLine { Text = "时间过期" } } } } } }; return body; }
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.宿主程序如下
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("你的名字时什么(5秒)"); string text = Console.ReadLine();
application.ResumeBookmark(bookmarkName, text); completedEvent.WaitOne(); Console.WriteLine("工作流执行完成"); Console.ReadLine(); }
作者:生鱼片
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
2008-11-04 参加Tech.ED2008(微软技术大会)上海站