WorkflowInvoker.InvokeAsync Method (TimeSpan, Object)

.NET Framework (current version)
 

Invokes a workflow asynchronously with the specified time-out interval and a unique identifier.

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

public void InvokeAsync(
	TimeSpan timeout,
	object userState
)

Parameters

timeout
Type: System.TimeSpan

The interval in which the workflow must complete before it is aborted and a TimeoutException is thrown.

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 userState 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. If the workflow does not complete within the specified time-out interval the workflow is aborted and a TimeoutException is thrown.

System_CAPS_noteNote

The TimeoutException is only thrown if the time-out interval elapses and the workflow becomes idle during execution. A workflow that takes longer than the specified time-out interval to complete completes successfully if the workflow does not become idle.

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: