Partage via


WorkflowInvoker.CancelAsync(Object) Méthode

Définition

Tente d’annuler le flux de travail qui a été appelé avec le flux de travail spécifié userState.

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

Paramètres

userState
Object

Jeton de l’annulation du flux de travail.

Exemples

L’exemple suivant appelle un flux de travail constitué d’une LongRunningDiceRoll activité. L’activité LongRunningDiceRoll a deux arguments de sortie qui représentent les résultats de l’opération de lancement de dés. Une fois le flux de travail appelé, l’hôte tente d’annuler le flux de travail.

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)
    {
        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.");

Remarques

Seul un flux de travail appelé par l’une des InvokeAsync surcharges qui acceptent un userState paramètre peut être annulé.

Si l’annulation réussit, la Cancelled propriété du InvokeCompletedEventArgs gestionnaire passé InvokeCompleted est définie truesur ; sinon, elle est définie falsesur .

S’applique à