Share via


WorkflowInvoker.CancelAsync(Object) Método

Definição

As tentativas de cancelar o fluxo de trabalho que foi invocado com o userState especificado.

public:
 void CancelAsync(System::Object ^ userState);
public void CancelAsync (object userState);
member this.CancelAsync : obj -> unit
Public Sub CancelAsync (userState As Object)

Parâmetros

userState
Object

O token do fluxo de trabalho a ser cancelado.

Exemplos

O exemplo a seguir invoca um fluxo de trabalho que consiste em uma LongRunningDiceRoll atividade . A atividade de LongRunningDiceRoll tem dois argumentos de saída que representam os resultados da operação de rolagem de dados. Depois que o fluxo de trabalho é invocado, o host tenta cancelar o fluxo de trabalho.

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}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

string userState = "CancelAsync Example";
invoker.InvokeAsync(userState);

Console.WriteLine("Waiting for the workflow to complete.");
Thread.Sleep(TimeSpan.FromSeconds(1));

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

// Wait for the workflow to complete.
syncEvent.WaitOne();

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

Comentários

Somente um fluxo de trabalho invocado por uma das InvokeAsync sobrecargas que usa um userState parâmetro pode ser cancelado.

Se o cancelamento for bem-sucedido, a Cancelled propriedade do InvokeCompletedEventArgs passada para o InvokeCompleted manipulador será definida truecomo ; caso contrário, ela será definida falsecomo .

Aplica-se a