從一個工作流異步啟動另一個工作流。啟動後立即完成,不等待子工作流結束。
TargetWorkflow 為目標工作流類型。建立子工作流實例前觸發,可完成初始化設定。
InvokeWorkflowActivityDemo。
SubWF。
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("這是發自子狀態機中的消息。");
}
在 State1 的 StateInitialization 中放入 InvokeWorkflowActivity,設定 TargetWorkflow 為 SubWF。
在 State2 的 EventDriven 中放入 HandleExternalEventActivity,等待本地服務事件通知子工作流完成。
設定 InterfaceType 為自訂介面,EventName 為 InvokedWorkflowComplete。
[ExternalDataExchange]
internal interface ILocalService
{
event EventHandler<ExternalDataEventArgs> InvokedWorkflowComplete;
void WorkComplete(Guid HostWFGuid);
}
internal class LocalService : ILocalService
{
public event EventHandler<ExternalDataEventArgs> InvokedWorkflowComplete;
public void WorkComplete(Guid HostWFGuid)
{
if (InvokedWorkflowComplete != null)
InvokedWorkflowComplete(null, new ExternalDataEventArgs(HostWFGuid));
}
}
using System;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;
namespace InvokeWorkflowActivityDemo
{
class Program
{
static Guid HostWFGuid;
static LocalService ls;
static void Main(string[] args)
{
using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
// 載入本地服務
ExternalDataExchangeService dataService = new ExternalDataExchangeService();
workflowRuntime.AddService(dataService);
ls = new LocalService();
dataService.AddService(ls);
AutoResetEvent waitHandle = new AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)
{
if (e.WorkflowInstance.InstanceId != HostWFGuid)
{
// 子工作流完成,通知主工作流
ls.WorkComplete(HostWFGuid);
}
else
{
// 主工作流完成
waitHandle.Set();
}
};
workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
};
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
HostWFGuid = instance.InstanceId;
instance.Start();
waitHandle.WaitOne();
Console.ReadLine();
}
}
}
}
https://zhuxianzhong.blog.51cto.com/157061/203672