工作流学习笔记-给工作流传递参数

2022-05-09 11:43:52 浏览数 (1)

C#->控制台顺序工作流->拖放code->双击生成事件处理程序

代码语言:javascript复制
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace WorkflowConsoleApplication1
{
	public sealed partial class Workflow1: SequentialWorkflowActivity
	{
        public string MyName { get; set; }

        public int MyID { get; set; }
		public Workflow1()
		{
			InitializeComponent();
		}

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(600);
            Console.WriteLine("My ID Is :{0}", this.MyID);            
            Console.WriteLine("My Name Is :{0}", this.MyName);            
        }
	}

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;

namespace WorkflowConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                AutoResetEvent waitHandle = new AutoResetEvent(false);
                //工作流完成时触发
                workflowRuntime.WorkflowCompleted  = delegate(object sender, WorkflowCompletedEventArgs e) 
                {
                    //通知waitHandle,释放控制台应用程序
                    waitHandle.Set();
                };
                //工作流发生错误时触发
                workflowRuntime.WorkflowTerminated  = delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };

                //构造参数
                Dictionary<string, object> wf_args = new Dictionary<string, object>();
                wf_args.Add("MyID", 1);
                wf_args.Add("MyName", "allen");

                //创建工作流的实例并启动工作流
                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication1.Workflow1), wf_args);
                instance.Start();

                //让控制台等待工作流的完成
                waitHandle.WaitOne();

                Console.ReadKey();
            }
        }
    }
}

0 人点赞