WorkflowInvoker.CancelAsync(Object) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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.
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}",
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.");
Only a workflow invoked by one of the InvokeAsync overloads that takes a userState
parameter can be canceled.
If the cancellation succeeds, the Cancelled property of the InvokeCompletedEventArgs passed to the InvokeCompleted handler is set to true
; otherwise, it is set to false
.
Προϊόν | Εκδόσεις |
---|---|
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET σχόλια
.NET είναι ένα έργο ανοιχτού κώδικα. Επιλέξτε μια σύνδεση για να παρέχετε σχόλια: