WorkflowApplication.Terminate Method (Exception)

.NET Framework (current version)
 

Terminates a workflow instance using the specified exception.

Namespace:   System.Activities
Assembly:  System.Activities (in System.Activities.dll)

public void Terminate(
	Exception reason
)

Parameters

reason
Type: System.Exception

The reason for terminating the workflow instance.

This method schedules the termination of the workflow instance. To be notified when the termination has completed, use the P:System.Activities.WorkflowApplication.OnCompleted handle.

By default, the Terminate operation must complete in 30 seconds or a TimeoutException is thrown.

The following example hosts a workflow using WorkflowApplication. A WorkflowApplication instance is constructed using the specified workflow definition, the desired workflow lifecycle events are handled, and the workflow is invoked with a call to Run. After the workflow is started, Terminate is called. When the workflow is terminated, the following output is displayed to the console.

Starting the workflow. Workflow e6b33409-f010-49f1-82ce-56f8baabe5e5 Terminated.Exception: System.ApplicationExceptionTerminating 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."));

.NET Framework
Available since 4.0
Return to top
Show: