Bookmark与CodeActivity的区别
当工作流运行至Bookmark时,Bookmark会让工作流在此处挂起(Idel),是线程挂起,如果是Web请求处理程序执行,将不会返回,等待恢复。而Code不会挂起;
Code继承自CodeActivity,而Bookmark需继承自NativeActivity;
设计一个bookmark1.cs
using System; using System.Activities; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public sealed class Bookmark1<T> : NativeActivity<T> { public InArgument<string> InParam { get; set; } protected override bool CanInduceIdle { get { return true; } } protected override void Execute(NativeActivityContext context) { this.Result.Set(context, "Borkmark1:结果传出"); context.CreateBookmark("Borkmark1", new BookmarkCallback(bookmarkCallback)); } //恢复运行时的回调函数 void bookmarkCallback(NativeActivityContext context, Bookmark bookmark, object obj) { MessageBox.Show("Borkmark1:恢复运行,传入的参数是:" + obj); //接收到的参数 this.Result.Set(context, (T)obj); } } }
编译,设计一个activity.xaml
设计一下winform
using System; using System.Activities; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } WorkflowApplication instance = null; private void button1_Click(object sender, EventArgs e) { instance = new WorkflowApplication(new Activity1()); instance.OnUnhandledException = unhandledExceptionl; instance.Aborted = aborted; instance.Idle = workflowIdel; instance.Completed = workflowCompleted; instance.Run(); } private void button2_Click(object sender, EventArgs e) { if (instance.GetBookmarks().Count() == 1) { BookmarkResumptionResult BRR = instance.ResumeBookmark(instance.GetBookmarks()[0].BookmarkName, "inPut"); MessageBox.Show("Bookmark恢复执行:" + BRR.ToString()); } } void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { MessageBox.Show("完成!"); } void aborted(WorkflowApplicationAbortedEventArgs e) { MessageBox.Show("中止!"); } UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e) { MessageBox.Show("异常!"); return UnhandledExceptionAction.Cancel; } void workflowIdel(WorkflowApplicationIdleEventArgs e) { MessageBox.Show("挂起!"); } } }
其执行效果为:
点击启动工作流 => 弹出"挂起" => 点击恢复运行 => 弹出"Bookmark恢复运行:Success" => 弹出"恢复运行,传入的参数是inPut" => 弹出"完成"
基本上运行一次就知道这种执行顺序了。
BookMark是一个非常重要的工具,它能够暂停工作流的执行,让工作流进入空闲状态。这在状态机工作流中是非常有用的。尤其状态机与MVC结合实现会签功能的时候,非常完美。有待再次验证一下实际场景。

