Running Workflows

Workflow instances can be started in two ways: Through workflow types or through XAML-based workflow markup.

To start a workflow instance through a workflow type, call the CreateWorkflow method, passing in the System.Type of the workflow, and then call Start.

Dim workflowInstance As WorkflowInstance
workflowInstance = workflowRuntime.CreateWorkflow(GetType(Workflow1))
workflowInstance.Start()
WorkflowInstance instance = workflowRuntime.CreateWorkflow
    (typeof(WorkflowApplication.Workflow1));
instance.Start();

To start a workflow instance using workflow markup only, call the CreateWorkflow method, passing in the XmlReader containing the workflow definition in either a file or stream, and then call Start.

Dim workflowInstance As WorkflowInstance
workflowInstance = workflowRuntime.CreateWorkflow(workflowDefinitionReader)
workflowInstance.Start()
WorkflowInstance instance = workflowRuntime.CreateWorkflow(workflowDefinitionReader);
instance.Start();

Note

If you are using a workflow markup file with a code-separation file, you must pass the workflow type into CreateWorkflow instead of passing in the workflow markup file.

By default, workflows are started asynchronously by the Windows Workflow runtime engine. To make sure that your host application does not close before your workflow has finished executing, you must use synchronizing threading objects that are provided by the .NET Framework, such as the AutoResetEvent object. The following code example shows how to create and start the workflow runtime, start a workflow instance, and exit by using an AutoResetEvent when the WorkflowRuntime object raises the WorkflowCompleted event.

Class Program

    Shared WaitHandle As New AutoResetEvent(False)

    Shared Sub Main()
        Using workflowRuntime As New WorkflowRuntime()
            AddHandler workflowRuntime.WorkflowCompleted, AddressOf OnWorkflowCompleted

            Dim workflowInstance As WorkflowInstance
            workflowInstance = workflowRuntime.CreateWorkflow(GetType(Workflow1))
            workflowInstance.Start()
            WaitHandle.WaitOne()
        End Using
    End Sub

    Shared Sub OnWorkflowCompleted(ByVal sender As Object, ByVal e As WorkflowCompletedEventArgs)
        WaitHandle.Set()
    End Sub
End Class
static void Main(string[] args)
{
    using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
    {
        AutoResetEvent waitHandle = new AutoResetEvent(false);

        workflowRuntime.WorkflowCompleted += delegate(object sender,
            WorkflowCompletedEventArgs e)
        { 
            waitHandle.Set(); 
        };
        
        WorkflowInstance instance = workflowRuntime.CreateWorkflow
            (typeof(WorkflowApplication.Workflow1));

        instance.Start();

        waitHandle.WaitOne();
    }
}

Windows Workflow Foundation does not have any restrictions on the execution environment of the hosting application. For instance, some host application environments may require that several processes execute in several application domains, each with its own threading model independent of the other executing processes. In this way, Windows Workflow Foundation remains portable and extensible as underlying hosting architectures change.

Note

Windows Workflow Foundation uses the .NET thread pool. If your host application is multi-threaded and uses the .NET thread pool extensively, you may starve the .NET thread pool. This could cause timeouts when a persistence service tries to complete a persistence transaction because Transaction objects also use the .NET thread pool.

See Also

Concepts

Creating the WorkflowRuntime
Processing WorkflowRuntime Events