WorkflowApplication.Run 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開始或繼續執行工作流程執行個體。
多載
Run() |
開始或繼續執行工作流程執行個體。 |
Run(TimeSpan) |
開始或繼續執行使用指定逾時間隔的工作流程執行個體。 |
備註
呼叫此方法以啟始執行最新建立的工作流程執行個體。
Run()
開始或繼續執行工作流程執行個體。
public:
void Run();
public void Run ();
member this.Run : unit -> unit
Public Sub Run ()
範例
下列範例會使用 WorkflowApplication 來裝載工作流程。 系統會使用指定的工作流程定義來建構 WorkflowApplication 執行個體、處理所需的工作流程開發週期事件,並且透過呼叫 Run 叫用此工作流程。 當工作流程完成時,主控台就會顯示下列輸出。
Starting the workflow.
Workflow 593976e8-558d-4989-94d6-50a14b34fd7b Idle.
Ending the workflow.
Workflow 593976e8-558d-4989-94d6-50a14b34fd7b Completed
Workflow 593976e8-558d-4989-94d6-50a14b34fd7b Unloaded.
Activity wf = new Sequence
{
Activities =
{
new WriteLine
{
Text = "Starting the workflow."
},
new Delay
{
Duration = TimeSpan.FromSeconds(5)
},
new WriteLine
{
Text = "Ending the workflow."
}
}
};
// Create a WorkflowApplication instance.
WorkflowApplication wfApp = new WorkflowApplication(wf);
// Subscribe to any desired workflow lifecycle events.
wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
if (e.CompletionState == ActivityInstanceState.Faulted)
{
Console.WriteLine("Workflow {0} Terminated.", e.InstanceId);
Console.WriteLine("Exception: {0}\n{1}",
e.TerminationException.GetType().FullName,
e.TerminationException.Message);
}
else if (e.CompletionState == ActivityInstanceState.Canceled)
{
Console.WriteLine("Workflow {0} Canceled.", e.InstanceId);
}
else
{
Console.WriteLine("Workflow {0} Completed.", e.InstanceId);
// Outputs can be retrieved from the Outputs dictionary,
// keyed by argument name.
// Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]);
}
};
wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
{
// Display the exception that caused the workflow
// to abort.
Console.WriteLine("Workflow {0} Aborted.", e.InstanceId);
Console.WriteLine("Exception: {0}\n{1}",
e.Reason.GetType().FullName,
e.Reason.Message);
};
wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e)
{
// Perform any processing that should occur
// when a workflow goes idle. If the workflow can persist,
// both Idle and PersistableIdle are called in that order.
Console.WriteLine("Workflow {0} Idle.", e.InstanceId);
};
wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
{
// Instruct the runtime to persist and unload the workflow
return PersistableIdleAction.Unload;
};
wfApp.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
Console.WriteLine("Workflow {0} Unloaded.", e.InstanceId);
};
wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
{
// Display the unhandled exception.
Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
e.InstanceId, e.UnhandledException.Message);
Console.WriteLine("ExceptionSource: {0} - {1}",
e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);
// Instruct the runtime to terminate the workflow.
// Other choices are Abort and Cancel
return UnhandledExceptionAction.Terminate;
};
// Run the workflow.
wfApp.Run();
備註
呼叫此方法以啟始執行最新建立的工作流程執行個體。
如果執行作業沒有在 30 秒內完成,則會擲回 TimeoutException。
適用於
Run(TimeSpan)
開始或繼續執行使用指定逾時間隔的工作流程執行個體。
public:
void Run(TimeSpan timeout);
public void Run (TimeSpan timeout);
member this.Run : TimeSpan -> unit
Public Sub Run (timeout As TimeSpan)
參數
範例
下列範例會使用 WorkflowApplication 來裝載工作流程。 系統會使用指定的工作流程定義來建構 WorkflowApplication 執行個體、處理所需的工作流程開發週期事件,並且透過呼叫 Run 叫用此工作流程。 當工作流程完成時,主控台就會顯示下列輸出。
Starting the workflow.
Workflow 593976e8-558d-4989-94d6-50a14b34fd7b Idle.
Ending the workflow.
Workflow 593976e8-558d-4989-94d6-50a14b34fd7b Completed
Workflow 593976e8-558d-4989-94d6-50a14b34fd7b Unloaded.
Activity wf = new Sequence
{
Activities =
{
new WriteLine
{
Text = "Starting the workflow."
},
new Delay
{
Duration = TimeSpan.FromSeconds(5)
},
new WriteLine
{
Text = "Ending the workflow."
}
}
};
// Create a WorkflowApplication instance.
WorkflowApplication wfApp = new WorkflowApplication(wf);
// Subscribe to any desired workflow lifecycle events.
wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
if (e.CompletionState == ActivityInstanceState.Faulted)
{
Console.WriteLine("Workflow {0} Terminated.", e.InstanceId);
Console.WriteLine("Exception: {0}\n{1}",
e.TerminationException.GetType().FullName,
e.TerminationException.Message);
}
else if (e.CompletionState == ActivityInstanceState.Canceled)
{
Console.WriteLine("Workflow {0} Canceled.", e.InstanceId);
}
else
{
Console.WriteLine("Workflow {0} Completed.", e.InstanceId);
// Outputs can be retrieved from the Outputs dictionary,
// keyed by argument name.
// Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]);
}
};
wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
{
// Display the exception that caused the workflow
// to abort.
Console.WriteLine("Workflow {0} Aborted.", e.InstanceId);
Console.WriteLine("Exception: {0}\n{1}",
e.Reason.GetType().FullName,
e.Reason.Message);
};
wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e)
{
// Perform any processing that should occur
// when a workflow goes idle. If the workflow can persist,
// both Idle and PersistableIdle are called in that order.
Console.WriteLine("Workflow {0} Idle.", e.InstanceId);
};
wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
{
// Instruct the runtime to persist and unload the workflow
return PersistableIdleAction.Unload;
};
wfApp.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
Console.WriteLine("Workflow {0} Unloaded.", e.InstanceId);
};
wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
{
// Display the unhandled exception.
Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
e.InstanceId, e.UnhandledException.Message);
Console.WriteLine("ExceptionSource: {0} - {1}",
e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);
// Instruct the runtime to terminate the workflow.
// Other choices are Abort and Cancel
return UnhandledExceptionAction.Terminate;
};
// Run the workflow.
wfApp.Run();
備註
請注意,不同於 Invoke,這個方法只會在工作流程未在指定的時間內啟動時逾時,而不是在必須於指定的時間內完成卻未完成時逾時。 這是因為 Invoke 會以同步方式 (封鎖主執行緒) 執行工作流程,而 Run 是以非同步方式執行,即只封鎖主執行緒到工作流程啟動為止。