在宿主中使用参数与实例通信
1.很多时候,宿主要与工作流实例的内部对象进行通信,比如启动时,要设定某些属性的值,完成时要读取某些属性的值。
工作流设计如下:
![](https://images.cnblogs.com/cnblogs_com/foundation/Parameter.JPG)
工作流代码
Public class Workflow1
Inherits SequentialWorkflowActivity
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'------------------------年龄属性----------------------------------
Private 年龄_value As Integer '存值变量
Public Property 年龄() As Integer '属性
Get
年龄= 年龄_value
End Get
Set(ByVal value As Integer)
年龄_value = value
End Set
End Property
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'------------------------年龄类型属性------------------------------
Private 年龄类型_value As 年龄状况 '存值变量
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Enum 年龄状况 '年龄状况枚举,年龄类型属性的值
成年
未成年
End Enum
Public Property 年龄类型() As 年龄状况 '属性
Get
年龄类型= 年龄类型_value
End Get
Set(ByVal value As 年龄状况)
年龄类型_value = value
End Set
End Property
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'------------------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)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'-------------定义Dictionary类型,传参用------------------------------
Dim 参数As System.Collections.Generic.Dictionary(Of String, Object)
参数= New System.Collections.Generic.Dictionary(Of String, Object)()
参数.Add("年龄", nl)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'参数.Add("名", 值) '可添加多个
'----------------------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim workflowInstance As WorkflowInstance
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'使用CreateWorkflow方法带传参的重载
workflowInstance = workflowRuntime.CreateWorkflow(GetType(Workflow1), 参数)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
workflowInstance.Start()
WaitHandle.WaitOne()
End Using
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.Read()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'完成时的回调
Shared Sub OnWorkflowCompleted(ByVal sender As Object, ByVal e As WorkflowCompletedEventArgs)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'e.OutputParameters可以从工作流中将属性值取出
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim value As String = e.OutputParameters("年龄类型").ToString()
Console.WriteLine("年龄类型: " + value)
Console.WriteLine(e.OutputParameters("年龄"))
WaitHandle.Set()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'出错时的回调
Shared Sub OnWorkflowTerminated(ByVal sender As Object, ByVal e As WorkflowTerminatedEventArgs)
Console.WriteLine(e.Exception.Message)
WaitHandle.Set()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Class
本例中是使用CreateWorkflow(工作流, 参数)的方式,在创建工作流时,对工作流的[年龄]属性赋值
当工作流实例无成后,在OnWorkflowCompleted事件的WorkflowCompletedEventArgs参数中将属性值读出
2.宿主与实例的通信还可以通过[外部事件]、[持久化]等方式实现,另外也可自已设计通信信道。
例子源码:例_参数Parameter工作流设计如下:
工作流代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
宿主代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)