CancelAsync Method
Collapse the table of content
Expand the table of content

WorkflowInvoker.CancelAsync Method (Object)

.NET Framework 4.6 and 4.5

Attempts to cancel the workflow that was invoked with the specified userState.

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

public void CancelAsync(
	object userState


Type: System.Object

The token for the workflow to cancel.

Only a workflow invoked by one of the InvokeAsync overloads that takes a userState parameter can be canceled.

If the cancellation succeeds, the P:System.ComponentModel.Cancelled property of the InvokeCompletedEventArgs passed to the InvokeCompleted handler is set to true; otherwise, it is set to false.

The following example invokes a workflow consisting of a LongRunningDiceRoll activity. The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. Once the workflow is invoked, the host attempts to cancel the workflow.

public sealed class LongRunningDiceRoll : Activity
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
        this.Implementation = () => new Sequence
            Activities =
                new WriteLine
                    Text = "Rolling the dice for 5 seconds."
                new Delay
                    Duration = TimeSpan.FromSeconds(5)
                new DiceRoll
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
    if (args.Cancelled == true)
        Console.WriteLine("The workflow was cancelled.");
    else if (args.Error != null)
        Console.WriteLine("Exception: {0}\n{1}",
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);


string userState = "CancelAsync Example";

Console.WriteLine("Waiting for the workflow to complete.");

Console.WriteLine("Attempting to cancel the workflow.");

// Wait for the workflow to complete.

Console.WriteLine("The workflow is either completed or cancelled.");

.NET Framework
Available since 4.0
Return to top
© 2015 Microsoft