1. 程式人生 > >有返回值的Bookmark

有返回值的Bookmark

AI text activit static 測試 變量 mage res ebo

首先代碼創建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); } }

有返回值的Bookmark