Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WorkflowApplication.Terminate Method (String, TimeSpan)

Terminates a workflow instance using the specified error message and time-out interval.

Namespace:  System.Activities
Assembly:  System.Activities (in System.Activities.dll)
member Terminate : 
        reason:string * 
        timeout:TimeSpan -> unit 

Parameters

reason
Type: System.String
The reason for terminating the workflow instance.
timeout
Type: System.TimeSpan
The interval in which the Terminate(String, TimeSpan) operation must complete before the operation is canceled and a TimeoutException is thrown.

This method schedules the termination of the workflow instance. To be notified when the termination has completed, use the OnCompleted() handle.

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(String, TimeSpan) is called. When the workflow is terminated, the following output is displayed to the console.

Starting the workflow. Workflow 2897d2ef-377e-4224-ae93-5c19b38f487c Terminated. Exception: System.Activities.WorkflowApplicationTerminatedExceptionTerminating 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));


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.