首先代码创建Activity:
public sealed class WaitForResponse<TResult>:NativeActivity<TResult> { public WaitForResponse() :base() {} public string ResponseName { get; set; } protected override bool CanInduceIdle => true; protected override void Execute(NativeActivityContext context) { context.CreateBookmark(this.ResponseName, new BookmarkCallback(this.ReceivedResponse)); //这里加入BookmarkCallback, 用来设置返回值 } void ReceivedResponse(NativeActivityContext context,Bookmark bookmark,object obj) { this.Result.Set(context, (TResult)obj); } }
创建Workflow:
在Branch1中创建一个变量myVar, 然后在创建的Activity中设置Result为myVar, 这个myVar会接收这个Activity创建Bookmark时返回的值.
测试:
public partial class Form1 : Form { static WorkflowApplication wfapp; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { wfapp = new WorkflowApplication(new ActivityLibrary1.Activity1()); wfapp.Idle = MyIdle; wfapp.Run(); ThreadPool.QueueUserWorkItem(ReadName); //传入的关键, 不然workflow不会resume, Pick会运行到Branch2中去 } void MyIdle(WorkflowApplicationIdleEventArgs e) { Console.WriteLine("Idle:" + e.InstanceId); } static void ReadName(object state) { string text = "abc"; // Resume the Activity that set this bookmark (ReadString). Form1.wfapp.ResumeBookmark("P1", text); } }