WorkflowApplication.Terminate 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
终止工作流实例。
重载
Terminate(Exception) |
使用指定的异常终止工作流实例。 |
Terminate(String) |
使用指定的错误消息终止工作流实例。 |
Terminate(Exception, TimeSpan) |
使用指定的异常和超时间隔终止工作流实例。 |
Terminate(String, TimeSpan) |
使用指定的错误消息和超时间隔终止工作流实例。 |
Terminate(Exception)
使用指定的异常终止工作流实例。
public:
void Terminate(Exception ^ reason);
public void Terminate (Exception reason);
member this.Terminate : Exception -> unit
Public Sub Terminate (reason As Exception)
参数
- reason
- Exception
终止工作流实例的原因。
示例
下面的示例使用 WorkflowApplication 承载一个工作流。 该示例使用指定的工作流定义构造 WorkflowApplication,处理所需的工作流生命周期事件,并通过调用 Run 来调用工作流。 启动工作流之后,将调用 Terminate。 终止工作流时,会向控制台显示以下输出。
Starting the workflow.
Workflow e6b33409-f010-49f1-82ce-56f8baabe5e5 Terminated.
Exception: System.ApplicationException
Terminating the workflow.
Workflow e6b33409-f010-49f1-82ce-56f8baabe5e5 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.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
Console.WriteLine("Workflow {0} unloaded.", e.InstanceId);
};
// Run the workflow.
wfApp.Run();
Thread.Sleep(TimeSpan.FromSeconds(1));
wfApp.Terminate(new ApplicationException("Terminating the workflow."));
注解
此方法会安排对工作流实例的终止操作。 若要在终止完成后得到通知,请使用 Completed 处理程序。
默认情况下,Terminate 操作必须在 30 秒内完成,否则将引发 TimeoutException。
适用于
Terminate(String)
使用指定的错误消息终止工作流实例。
public:
void Terminate(System::String ^ reason);
public void Terminate (string reason);
member this.Terminate : string -> unit
Public Sub Terminate (reason As String)
参数
- reason
- String
终止工作流实例的原因。
示例
下面的示例使用 WorkflowApplication 承载一个工作流。 该示例使用指定的工作流定义构造 WorkflowApplication,处理所需的工作流生命周期事件,并通过调用 Run 来调用工作流。 启动工作流之后,将调用 Terminate。 终止工作流时,会向控制台显示以下输出。
Starting the workflow.
Workflow f87c6f91-4fe4-40b9-b7cb-4f1bd071bf84 Terminated.
Exception: System.Activities.WorkflowApplicationTerminatedException
Terminating the workflow.
Workflow f87c6f91-4fe4-40b9-b7cb-4f1bd071bf84 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.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
Console.WriteLine("Workflow {0} unloaded.", e.InstanceId);
};
// Run the workflow.
wfApp.Run();
Thread.Sleep(TimeSpan.FromSeconds(1));
wfApp.Terminate("Terminating the workflow.");
注解
此方法会安排对工作流实例的终止操作。 若要在终止完成后得到通知,请使用 Completed 处理程序。
默认情况下,Terminate 操作必须在 30 秒内完成,否则将引发 TimeoutException。
适用于
Terminate(Exception, TimeSpan)
使用指定的异常和超时间隔终止工作流实例。
public:
void Terminate(Exception ^ reason, TimeSpan timeout);
public void Terminate (Exception reason, TimeSpan timeout);
member this.Terminate : Exception * TimeSpan -> unit
Public Sub Terminate (reason As Exception, timeout As TimeSpan)
参数
- reason
- Exception
终止工作流实例的原因。
- timeout
- TimeSpan
必须在此时间内完成 Terminate(Exception, TimeSpan) 操作的时间间隔,否则,将取消该操作并引发 TimeoutException。
示例
下面的示例使用 WorkflowApplication 承载一个工作流。 该示例使用指定的工作流定义构造 WorkflowApplication,处理所需的工作流生命周期事件,并通过调用 Run 来调用工作流。 启动工作流之后,将调用 Terminate。 终止工作流时,会向控制台显示以下输出。
Starting the workflow.
Workflow de28efe5-9057-472b-8d95-899c249893c5 Terminated.
Exception: System.ApplicationException
Terminating the workflow.
Workflow de28efe5-9057-472b-8d95-899c249893c5 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.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
Console.WriteLine("Workflow {0} unloaded.", e.InstanceId);
};
// Run the workflow.
wfApp.Run();
Thread.Sleep(TimeSpan.FromSeconds(1));
wfApp.Terminate(new ApplicationException("Terminating the workflow."),
TimeSpan.FromSeconds(15));
注解
此方法会安排对工作流的终止操作。 若要在终止完成后得到通知,请使用 Completed 处理程序。
适用于
Terminate(String, TimeSpan)
使用指定的错误消息和超时间隔终止工作流实例。
public:
void Terminate(System::String ^ reason, TimeSpan timeout);
public void Terminate (string reason, TimeSpan timeout);
member this.Terminate : string * TimeSpan -> unit
Public Sub Terminate (reason As String, timeout As TimeSpan)
参数
- reason
- String
终止工作流实例的原因。
- timeout
- TimeSpan
必须在此时间内完成 Terminate(String, TimeSpan) 操作的时间间隔,否则,将取消该操作并引发 TimeoutException。
示例
下面的示例使用 WorkflowApplication 承载一个工作流。 该示例使用指定的工作流定义构造 WorkflowApplication,处理所需的工作流生命周期事件,并通过调用 Run 来调用工作流。 启动工作流之后,将调用 Terminate。 终止工作流时,会向控制台显示以下输出。
Starting the workflow.
Workflow 2897d2ef-377e-4224-ae93-5c19b38f487c Terminated.
Exception: System.Activities.WorkflowApplicationTerminatedException
Terminating the workflow.
Workflow 2897d2ef-377e-4224-ae93-5c19b38f487c 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.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
Console.WriteLine("Workflow {0} unloaded.", e.InstanceId);
};
// Run the workflow.
wfApp.Run();
Thread.Sleep(TimeSpan.FromSeconds(1));
wfApp.Terminate("Terminating the workflow.", TimeSpan.FromSeconds(15));
注解
此方法会安排对工作流实例的终止操作。 若要在终止完成后得到通知,请使用 Completed 处理程序。