Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Running Workflows

Running Workflows

.NET Framework 3.5

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.

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.

WorkflowInstance instance = workflowRuntime.CreateWorkflow(workflowDefinitionReader);
instance.Start();
Bb924456.note(en-us,VS.90).gifNote:
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.

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.

Bb924456.note(en-us,VS.90).gifNote:
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

Show:
© 2015 Microsoft