在宿主中使用参数与实例通信
1.很多时候,宿主要与工作流实例的内部对象进行通信,比如启动时,要设定某些属性的值,完成时要读取某些属性的值。
工作流设计如下:

工作流代码
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
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
本例中是使用CreateWorkflow(工作流, 参数)的方式,在创建工作流时,对工作流的[年龄]属性赋值
当工作流实例无成后,在OnWorkflowCompleted事件的WorkflowCompletedEventArgs参数中将属性值读出
2.宿主与实例的通信还可以通过[外部事件]、[持久化]等方式实现,另外也可自已设计通信信道。
例子源码:例_参数Parameter工作流设计如下:
工作流代码















































宿主代码


















































【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构