WorkflowInvoker.CancelAsync(Object) 方法

定義

嘗試取消使用指定之 userState 所叫用的工作流程。Attempts to cancel the workflow that was invoked with the specified 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

要取消之工作流程的權杖。The token for the workflow to cancel.

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。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}",
            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 多載所叫用之工作流程。Only a workflow invoked by one of the InvokeAsync overloads that takes a userState parameter can be canceled.

如果取消Cancelled成功,傳遞至InvokeCompleted處理常式之InvokeCompletedEventArgs的屬性會設定為true,否則會設定為falseIf the cancellation succeeds, the Cancelled property of the InvokeCompletedEventArgs passed to the InvokeCompleted handler is set to true; otherwise, it is set to false.

適用於