WorkflowInvoker.CancelAsync(Object) 方法

定义

尝试取消使用指定的 userState 调用的工作流。

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

参数

userState
Object

要取消的工作流的标记。

示例

下面的示例调用包含单个 LongRunningDiceRoll 活动的工作流。 LongRunningDiceRoll 活动包含两个表示掷骰子操作结果的输出自变量。 一旦调用了工作流,宿主将尝试取消工作流。

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

注解

只能取消由一种采用 InvokeAsync 参数的 userState 重载调用的工作流。

如果取消成功,则Cancelled传递给处理程序的 InvokeCompletedEventArgs 的 属性设置为 true;否则,它设置为 falseInvokeCompleted

适用于