Share via


Task 2: Create a Sequential Workflow

Download sample

For this task, you create a basic sequential workflow that is used throughout the rest of the tutorial. The workflow consists of two CodeActivity activities and a DelayActivity activity. The CodeActivity activities display messages in the system console window. A DelayActivity activity is used to insert a five-second pause between the two CodeActivity activities.

Note

Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following section.

To create the sequential workflow

  1. Create a new source file named HostingWorkflows.

    Use a .cs extension if you are creating a C# project, or a .vb extension if you are creating a Visual Basic project.

  2. Add the following directives to import the types that you must have for the sequential workflow.

    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Collections;
    using System.Drawing;
    using System.Workflow.Runtime;
    using System.Workflow.Activities;
    
  3. Create a new class named HostingWorkflows in the Microsoft.Samples.Workflow.Tutorials.Hosting namespace.

    Derive this class from the SequentialWorkflowActivity class.

    Namespace Microsoft.Samples.Workflow.Tutorials.Hosting
        Public NotInheritable Class HostingWorkflows : Inherits SequentialWorkflowActivity
        End Class
    End Namespace
    
    namespace Microsoft.Samples.Workflow.Tutorials.Hosting
    {
        public sealed class HostingWorkflows : SequentialWorkflowActivity
        {
        }
    }
    
  4. Declare two CodeActivity fields and one DelayActivity field in the HostingWorkflows class.

    private DelayActivity delayActivity1;
    private CodeActivity codeActivity2;
    private CodeActivity codeActivity1;
    
  5. Define the HostingWorkflow class constructor and call the InitializeComponent method that you created in the next step.

    public HostingWorkflows()
    {
        InitializeComponent();
    }
    
  6. Create the InitializeComponent method and set the CanModifyActivities property of the workflow to true.

  7. Create an instance of the two CodeActivity activities and the DelayActivity at the beginning of the method.

    private void InitializeComponent()
    {
        this.CanModifyActivities = true;
    
        this.codeActivity1 = new System.Workflow.Activities.CodeActivity();
        this.delayActivity1 = new System.Workflow.Activities.DelayActivity();
        this.codeActivity2 = new System.Workflow.Activities.CodeActivity();
    
  8. In the InitializeComponent method, create an event handler for the ExecuteCode event that is raised by each CodeActivity.

    Set the TimeoutDuration property for the DelayActivity to five seconds. Additionally, set the Name property for each activity by using the field name as the value.

    // 
    // codeActivity1
    // 
    this.codeActivity1.Name = "codeActivity1";
    this.codeActivity1.ExecuteCode +=
        new System.EventHandler(this.codeActivity1_ExecuteCode);
    // 
    // delayActivity1
    // 
    this.delayActivity1.Name = "delayActivity1";
    this.delayActivity1.TimeoutDuration = System.TimeSpan.Parse("00:00:05");
    // 
    // codeActivity2
    // 
    this.codeActivity2.Name = "codeActivity2";
    this.codeActivity2.ExecuteCode +=
        new System.EventHandler(this.codeActivity2_ExecuteCode);
    
  9. In the InitializeComponent method, add each workflow activity created in the previous steps to the Activities collection of the workflow.

    Note

    The order in which you add these activities should be codeActivity1, delayActivity1, and then codeActivity2.

  10. Additionally, set the Name property to "HostingWorkflows", and the CanModifyActivities property to false.

        // 
        // HostingWorkflows
        // 
        this.Activities.Add(this.codeActivity1);
        this.Activities.Add(this.delayActivity1);
        this.Activities.Add(this.codeActivity2);
        this.Name = "HostingWorkflows";
    
        this.CanModifyActivities = false;
    }
    
  11. In the HostingWorkflows class, create the event handler methods that are referenced in the previous step for the two CodeActivity activities.

    In each method, write a message to the system console.

    private void codeActivity1_ExecuteCode(object sender, EventArgs e)
    {
        Console.WriteLine("In codeActivity1_ExecuteCode ");
    }
    
    private void codeActivity2_ExecuteCode(object sender, EventArgs e)
    {
        Console.WriteLine("In codeActivity2_ExecuteCode ");
    }
    

To add the HostingWorkflows source file to the project

  1. In the HostingWorkflows project file that you created in the previous task, insert a new ItemGroup node before the Import nodes at the end of the project file.

  2. In the ItemGroup node, insert a new Compile node.

  3. Add an attribute named Include to the Compile node that you created in the previous step.

    The value of this attribute is the file name of the source file that you created in Task 1: Create the Workflow Hosting Project File.

  4. Add a child node to the Compile node named SubType.

    The value of this node is Component. The ItemGroup node appears similar to the following code.

    <ItemGroup>
        <Compile Include="HostingWorkflows.vb">
          <SubType>Component</SubType>
        </Compile>
    </ItemGroup>
    
    <ItemGroup>
        <Compile Include="HostingWorkflows.cs">
          <SubType>Component</SubType>
        </Compile>
    </ItemGroup>
    

Compiling the Code

At this point, the project does not compile because an application entry point has not been defined. This is discussed in the next task.

In Task 3: Create a Workflow Host Console Application, you create a console application that is used to host the Windows Workflow Foundation runtime engine.

See Also

Reference

SequentialWorkflowActivity
CodeActivity
DelayActivity

Concepts

How to: Compile Workflows
Workflow Authoring Styles

Other Resources

Task 3: Create a Workflow Host Console Application

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04