WorkflowEventArgs.WorkflowInstance 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取与工作流事件关联的工作流实例。
public:
property System::Workflow::Runtime::WorkflowInstance ^ WorkflowInstance { System::Workflow::Runtime::WorkflowInstance ^ get(); };
public System.Workflow.Runtime.WorkflowInstance WorkflowInstance { get; }
member this.WorkflowInstance : System.Workflow.Runtime.WorkflowInstance
Public ReadOnly Property WorkflowInstance As WorkflowInstance
属性值
与工作流事件关联的 WorkflowInstance。
示例
下面的代码示例演示如何在调用事件处理程序方法时使用 WorkflowInstance 属性来获取 WorkflowInstance 对象。 当 WorkflowIdled 事件发生时,调用本示例中定义的 OnWorkflowIdled
方法。 该方法使用 WorkflowInstance 属性来确定哪个工作流处于空闲状态,然后通过调用 GetWorkflowQueueData 方法来获取该工作流实例的排队项的集合。 该代码会循环访问该集合以确定哪个活动正在等待使工作流处于空闲状态的事件。 然后,该代码将使用 EnqueueItem 方法以及事件队列项的名称向工作流队列发出一个异常。
此代码示例摘自 Program.cs 文件中的“取消工作流”SDK 示例。 有关详细信息,请参阅 取消工作流。
static void OnWorkflowIdled(object sender, WorkflowEventArgs e)
{
WorkflowInstance workflow = e.WorkflowInstance;
Console.WriteLine("\n...waiting for 3 seconds... \n");
Thread.Sleep(3000);
// what activity is blocking the workflow
ReadOnlyCollection<WorkflowQueueInfo> wqi = workflow.GetWorkflowQueueData();
foreach (WorkflowQueueInfo q in wqi)
{
EventQueueName eq = q.QueueName as EventQueueName;
if (eq != null)
{
// get activity that is waiting for event
ReadOnlyCollection<string> blockedActivity = q.SubscribedActivityNames;
Console.WriteLine("Host: Workflow is blocked on " + blockedActivity[0]);
// this event is never going to arrive eg. employee left the company
// lets send an exception to this queue
// it will either be handled by exception handler that was modeled in workflow
// or the runtime will unwind running compensation handlers and exit the workflow
Console.WriteLine("Host: This event is not going to arrive");
Console.WriteLine("Host: Cancel workflow with unhandled exception");
workflow.EnqueueItem(q.QueueName, new Exception("ExitWorkflowException"), null, null);
}
}
}
Shared Sub OnWorkflowIdled(ByVal sender As Object, ByVal e As WorkflowEventArgs)
Dim workflow As WorkflowInstance = e.WorkflowInstance
Console.WriteLine(vbCrLf + "...waiting for 3 seconds... " + vbCrLf)
Thread.Sleep(3000)
' what activity is blocking the workflow
Dim wqi As ReadOnlyCollection(Of WorkflowQueueInfo) = workflow.GetWorkflowQueueData()
For Each q As WorkflowQueueInfo In wqi
Dim eq As EventQueueName = TryCast(q.QueueName, EventQueueName)
If eq IsNot Nothing Then
' get activity that is waiting for event
Dim blockedActivity As ReadOnlyCollection(Of String) = q.SubscribedActivityNames
Console.WriteLine("Host: Workflow is blocked on " + blockedActivity(0))
' this event is never going to arrive eg. employee left the company
' lets send an exception to this queue
' it will either be handled by exception handler that was modeled in workflow
' or the runtime will unwind running compensation handlers and exit the workflow
Console.WriteLine("Host: This event is not going to arrive")
Console.WriteLine("Host: Cancel workflow with unhandled exception")
workflow.EnqueueItem(q.QueueName, New Exception("ExitWorkflowException"), Nothing, Nothing)
End If
Next
End Sub