Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Run Method (TimeSpan)
Collapse the table of content
Expand the table of content

WorkflowApplication.Run Method (TimeSpan)

Begins or resumes the execution of a workflow instance using the specified time-out interval.

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

public void Run(
	TimeSpan timeout
)

Parameters

timeout
Type: System.TimeSpan
Call this method to initiate execution of a newly created workflow instance.
The interval in which the run operation must complete before the operation is canceled and a TimeoutException is thrown.

Note that unlike Invoke(TimeSpan), this method will time out only if the workflow doesn't start in the specified amount of time, rather than needing to complete in the specified amount of time. The reason for this is that Invoke(TimeSpan) executes the workflow synchronously (blocking the host thread), while Run(TimeSpan) executes asynchronously, only blocking the host thread for the amount of time it takes the workflow to start.

The following example hosts a workflow using WorkflowApplication. A WorkflowApplication instance is constructed using the specified workflow definition, the desired workflow lifecycle events are handled, and the workflow is invoked with a call to Run(). When the workflow is completed, the following output is displayed to the console.

Starting the workflow. Workflow 593976e8-558d-4989-94d6-50a14b34fd7b Idle. Ending the workflow. Workflow 593976e8-558d-4989-94d6-50a14b34fd7b CompletedWorkflow 593976e8-558d-4989-94d6-50a14b34fd7b Unloaded. 

Activity wf = new Sequence
{
    Activities =
     {
         new WriteLine
         {
             Text = "Starting the workflow."
         },
         new Delay
         {
             Duration = TimeSpan.FromSeconds(5)
         },
         new WriteLine
         {
             Text = "Ending the workflow."
         }
     }
};

// Create a WorkflowApplication instance.
WorkflowApplication wfApp = new WorkflowApplication(wf);

// Subscribe to any desired workflow lifecycle events.
wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
    if (e.CompletionState == ActivityInstanceState.Faulted)
    {
        Console.WriteLine("Workflow {0} Terminated.", e.InstanceId);
        Console.WriteLine("Exception: {0}\n{1}",
            e.TerminationException.GetType().FullName,
            e.TerminationException.Message);
    }
    else if (e.CompletionState == ActivityInstanceState.Canceled)
    {
        Console.WriteLine("Workflow {0} Canceled.", e.InstanceId);
    }
    else
    {
        Console.WriteLine("Workflow {0} Completed.", e.InstanceId);

        // Outputs can be retrieved from the Outputs dictionary,
        // keyed by argument name.
        // Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]);
    }
};

wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
{
    // Display the exception that caused the workflow
    // to abort.
    Console.WriteLine("Workflow {0} Aborted.", e.InstanceId);
    Console.WriteLine("Exception: {0}\n{1}",
        e.Reason.GetType().FullName,
        e.Reason.Message);
};

wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e)
{
    // Perform any processing that should occur
    // when a workflow goes idle. If the workflow can persist,
    // both Idle and PersistableIdle are called in that order.
    Console.WriteLine("Workflow {0} Idle.", e.InstanceId);
};

wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
{
    // Instruct the runtime to persist and unload the workflow
    return PersistableIdleAction.Unload;
};

wfApp.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
    Console.WriteLine("Workflow {0} Unloaded.", e.InstanceId);
};

wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
{
    // Display the unhandled exception.
    Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
        e.InstanceId, e.UnhandledException.Message);

    Console.WriteLine("ExceptionSource: {0} - {1}",
        e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);

    // Instruct the runtime to terminate the workflow.
    // Other choices are Abort and Cancel
    return UnhandledExceptionAction.Terminate;
};

// Run the workflow.
wfApp.Run();


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2015 Microsoft