WorkflowInvoker.InvokeAsync Method (Object)

.NET Framework (current version)
 

Invokes a workflow asynchronously using the specified unique identifier.

Namespace:   System.Activities
Assembly:  System.Activities (in System.Activities.dll)

public void InvokeAsync(
	object userState
)

Parameters

userState
Type: System.Object

A user-provided object used to distinguish this particular asynchronous invoke operation from other current asynchronous invoke operations.

The userState parameter must be unique across all currently running InvokeAsync operations for the current activity. If the userState parameter is not unique, an ArgumentException is thrown. userState is used to identify the workflow in InvokeCompleted, and to cancel the workflow using CancelAsync.

To be notified when the workflow is complete, handle InvokeCompleted. To configure a time-out interval in which the workflow must complete, use one of the InvokeAsync overloads that take a TimeSpan.

This method invokes a workflow asynchronously using the event-based asynchronous design pattern. For more information, seeEvent-based Asynchronous Pattern Overview.

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. When the workflow completes these are retrieved in the InvokeCompleted handler.

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("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();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

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

Console.WriteLine("The workflow is complete.");

.NET Framework
Available since 4.0
Return to top
Show: