Share via


Task 1: Listen For and Process EventsĀ 

Download sample

In this task, you learn how to interact with the Windows Workflow Foundation runtime engine during your application life cycle. Events are raised by the runtime engine during significant events. This lets you customize your application based on the current status of the runtime engine and any workflows that are executing.

This task focuses on handling the events that are raised by the runtime engine. Task 2: Suspend, Terminate, and Resume Workflows focuses on handling events that the runtime engine raises during the execution of a workflow.

NoteNote

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.

Adding Event Handlers

To add event handlers for the WorkflowRuntime.Started and WorkflowRuntime.Stopped events

  1. After the assignment of the workflowRuntime object in the Main method of your hosting application, create an event handler for the Started event that is defined on the workflowRuntime object.

    NoteNote

    To correctly handle the System.Workflow.Runtime.WorkflowRuntime.Started event, the event handler must be added before you start the runtime.

    ' workflow runtime events
    AddHandler workflowRuntime.Started, AddressOf workflowRuntime_Started
    
    // workflow runtime events
    workflowRuntime.Started += new 
        EventHandler<WorkflowRuntimeEventArgs>(workflowRuntime_Started);
    
  2. After the Started event handler, create an event handler for the Stopped event that is defined on the workflowRuntime object.

    AddHandler workflowRuntime.Stopped, AddressOf workflowRuntime_Stopped
    
    workflowRuntime.Stopped += new 
        EventHandler<WorkflowRuntimeEventArgs>(workflowRuntime_Stopped);
    
  3. In the Program class, create a new static method named workflowRuntime_Started.

    This method takes an Object named sender and a WorkflowRuntimeEventArgs object named e as parameters.

  4. Call the WriteLine method to write an informative message to the system console.

    Private Shared Sub workflowRuntime_Started _
              (ByVal sender As Object, ByVal e As WorkflowRuntimeEventArgs)
        Console.WriteLine("Workflow runtime started")
    End Sub
    
    static void workflowRuntime_Started(object sender, WorkflowRuntimeEventArgs e)
    {
        Console.WriteLine("Workflow runtime started");
    }
    
  5. In the Program class, create a new static method named workflowRuntime_Stopped.

    This method takes an Object named sender and a WorkflowRuntimeEventArgs object named e as parameters.

  6. Call the WriteLine method to write an informative message to the system console.

    Private Shared Sub workflowRuntime_Stopped(ByVal sender As Object, _
        ByVal e As WorkflowRuntimeEventArgs)
        Console.WriteLine("Workflow runtime stopped")
    End Sub
    
    static void workflowRuntime_Stopped(object sender, WorkflowRuntimeEventArgs e)
    {
        Console.WriteLine("Workflow runtime stopped");
    }
    

Compiling the Code

  1. Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.

  2. Go to the source directory of the tutorial.

  3. At the command prompt, type MSBUILD to build the project.

In Task 2: Suspend, Terminate, and Resume Workflows, you will learn how to create event handlers for specific workflow events.

See Also

Reference

WorkflowRuntime
Started
Stopped

Concepts

Creating a Workflow Host Application

Other Resources

Task 2: Suspend, Terminate, and Resume Workflows
Raise Event To Load Workflow

Footer image

Send comments about this topic to Microsoft.