转载
本文用C#浅析,用中文做名称为了练习用,实际开发不允许

工作流代码
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;
using System.Windows.Forms;

namespace WorkflowLibrary1


{
public sealed partial class Workflow1 : SequentialWorkflowActivity

{
public Workflow1()

{
InitializeComponent();
}

//------------------------年龄属性----------------------------------
private int 年龄_value;
public int 年龄

{
set

{
年龄_value = value;
}
get

{
return 年龄_value;
}
}

public enum 年龄状况

{
成年,
未成年,
}

//------------------------年龄类型属性------------------------------
private 年龄状况 年龄类型_value;
public 年龄状况 年龄类型

{
set

{
年龄类型_value = value;
}
get

{
return 年龄类型_value;
}
}


//------------------IfElse的两个分支----------------------
private void code_t_code(object sender, EventArgs e)

{
// >=18岁的
年龄类型 = 年龄状况.成年;
MessageBox.Show("年龄>=18的代码块,年龄:" + this.年龄.ToString());
}

private void code_f_code(object sender, EventArgs e)

{
// <18岁的
年龄类型 = 年龄状况.未成年;
MessageBox.Show("年龄<18的代码块,年龄:" + this.年龄.ToString());
}
}
}


我用的是窗体的,textBox1为输入的年龄文本框,按钮为“测试年龄段”

宿主程序代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Activities;

namespace WorkflowMonitorManagermentStudio


{
public partial class Form1 : Form

{
private WorkflowRuntime runtime = null;

private WorkflowInstance instance = null;

private static AutoResetEvent WaitHandle = new AutoResetEvent(false);

public Form1()

{
InitializeComponent();

runtime = new WorkflowRuntime();

runtime.StartRuntime();

runtime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(runtime_WorkflowCompleted);
runtime.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(runtime_WorkflowTerminated);
}

void runtime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)

{
MessageBox.Show(e.Exception.Message.ToString());
WaitHandle.Set();
}

void runtime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)

{
string value = e.OutputParameters["年龄类型"].ToString();
MessageBox.Show("年龄类型:" + value.ToString());
MessageBox.Show("年龄:" + e.OutputParameters["年龄"].ToString());
WaitHandle.Set();
}

private void button1_Click(object sender, EventArgs e)

{
if (textBox1.Text.Trim() == "")

{
MessageBox.Show("请输入年龄");
return;
}
else

{
Dictionary<string, object> 参数 = new Dictionary<string, object>();

参数.Add("年龄", Int32.Parse(textBox1.Text.Trim()));

instance = runtime.CreateWorkflow(typeof(WorkflowLibrary1.Workflow1), 参数);

instance.Start();
}
}
}
}

