本例中是使用CreateWorkflow(工作流, 参数)的方式,在创建工作流时,对工作流的[年龄]属性赋值
当工作流实例无成后,在OnWorkflowCompleted事件的WorkflowCompletedEventArgs参数中将属性值读出
2.宿主与实例的通信还可以通过[外部事件]、[持久化]等方式实现,另外也可自已设计通信信道。
例子源码:例_参数Parameter工作流设计如下:
工作流代码
Public class Workflow1
Inherits SequentialWorkflowActivity
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
'------------------------年龄属性----------------------------------
Private 年龄_value As Integer '存值变量
Public Property 年龄() As Integer '属性
Get
年龄= 年龄_value
End Get
Set(ByVal value As Integer)
年龄_value = value
End Set
End Property
'------------------------年龄类型属性------------------------------
Private 年龄类型_value As 年龄状况 '存值变量
Public Enum 年龄状况 '年龄状况枚举,年龄类型属性的值
成年
未成年
End Enum
Public Property 年龄类型() As 年龄状况 '属性
Get
年龄类型= 年龄类型_value
End Get
Set(ByVal value As 年龄状况)
年龄类型_value = value
End Set
End Property
'------------------IfElse的两个分支----------------------
'年龄>=18
Private Sub code_t_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
年龄类型= 年龄状况.成年
Console.WriteLine("年龄>=18的代码块,年龄:" & Me.年龄)
End Sub
'年龄<18
Private Sub code_f_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
年龄类型= 年龄状况.未成年
Console.WriteLine("年龄<18的代码块,年龄:" & Me.年龄)
End Sub
End Class
Inherits SequentialWorkflowActivity
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
'------------------------年龄属性----------------------------------
Private 年龄_value As Integer '存值变量
Public Property 年龄() As Integer '属性
Get
年龄= 年龄_value
End Get
Set(ByVal value As Integer)
年龄_value = value
End Set
End Property
'------------------------年龄类型属性------------------------------
Private 年龄类型_value As 年龄状况 '存值变量
Public Enum 年龄状况 '年龄状况枚举,年龄类型属性的值
成年
未成年
End Enum
Public Property 年龄类型() As 年龄状况 '属性
Get
年龄类型= 年龄类型_value
End Get
Set(ByVal value As 年龄状况)
年龄类型_value = value
End Set
End Property
'------------------IfElse的两个分支----------------------
'年龄>=18
Private Sub code_t_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
年龄类型= 年龄状况.成年
Console.WriteLine("年龄>=18的代码块,年龄:" & Me.年龄)
End Sub
'年龄<18
Private Sub code_f_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
年龄类型= 年龄状况.未成年
Console.WriteLine("年龄<18的代码块,年龄:" & Me.年龄)
End Sub
End Class
宿主代码
Class Program
Shared WaitHandle As New AutoResetEvent(False)
Shared Sub Main()
Console.Write("请输入年龄:")
Dim nl As Integer = Convert.ToInt32(Console.ReadLine())
Using workflowRuntime As New WorkflowRuntime()
AddHandler workflowRuntime.WorkflowCompleted, AddressOf OnWorkflowCompleted
AddHandler workflowRuntime.WorkflowTerminated, AddressOf OnWorkflowTerminated
'-------------定义Dictionary类型,传参用------------------------------
Dim 参数As System.Collections.Generic.Dictionary(Of String, Object)
参数= New System.Collections.Generic.Dictionary(Of String, Object)()
参数.Add("年龄", nl)
'参数.Add("名", 值) '可添加多个
'----------------------------------------------------------------------
Dim workflowInstance As WorkflowInstance
'使用CreateWorkflow方法带传参的重载
workflowInstance = workflowRuntime.CreateWorkflow(GetType(Workflow1), 参数)
workflowInstance.Start()
WaitHandle.WaitOne()
End Using
Console.Read()
End Sub
'完成时的回调
Shared Sub OnWorkflowCompleted(ByVal sender As Object, ByVal e As WorkflowCompletedEventArgs)
'e.OutputParameters可以从工作流中将属性值取出
Dim value As String = e.OutputParameters("年龄类型").ToString()
Console.WriteLine("年龄类型: " + value)
Console.WriteLine(e.OutputParameters("年龄"))
WaitHandle.Set()
End Sub
'出错时的回调
Shared Sub OnWorkflowTerminated(ByVal sender As Object, ByVal e As WorkflowTerminatedEventArgs)
Console.WriteLine(e.Exception.Message)
WaitHandle.Set()
End Sub
End Class
Shared WaitHandle As New AutoResetEvent(False)
Shared Sub Main()
Console.Write("请输入年龄:")
Dim nl As Integer = Convert.ToInt32(Console.ReadLine())
Using workflowRuntime As New WorkflowRuntime()
AddHandler workflowRuntime.WorkflowCompleted, AddressOf OnWorkflowCompleted
AddHandler workflowRuntime.WorkflowTerminated, AddressOf OnWorkflowTerminated
'-------------定义Dictionary类型,传参用------------------------------
Dim 参数As System.Collections.Generic.Dictionary(Of String, Object)
参数= New System.Collections.Generic.Dictionary(Of String, Object)()
参数.Add("年龄", nl)
'参数.Add("名", 值) '可添加多个
'----------------------------------------------------------------------
Dim workflowInstance As WorkflowInstance
'使用CreateWorkflow方法带传参的重载
workflowInstance = workflowRuntime.CreateWorkflow(GetType(Workflow1), 参数)
workflowInstance.Start()
WaitHandle.WaitOne()
End Using
Console.Read()
End Sub
'完成时的回调
Shared Sub OnWorkflowCompleted(ByVal sender As Object, ByVal e As WorkflowCompletedEventArgs)
'e.OutputParameters可以从工作流中将属性值取出
Dim value As String = e.OutputParameters("年龄类型").ToString()
Console.WriteLine("年龄类型: " + value)
Console.WriteLine(e.OutputParameters("年龄"))
WaitHandle.Set()
End Sub
'出错时的回调
Shared Sub OnWorkflowTerminated(ByVal sender As Object, ByVal e As WorkflowTerminatedEventArgs)
Console.WriteLine(e.Exception.Message)
WaitHandle.Set()
End Sub
End Class