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

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

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);           
        }
    }
 
}
 
<pre class="brush:csharp;gutter:false;">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();
            }
        }
    }
}
 
</pre>
posted @   liulun  阅读(1118)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示