Поделиться через


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.

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 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.

    Note

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

    // 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.

    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.

    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.

    static void workflowRuntime_Stopped(object sender, WorkflowRuntimeEventArgs e)
    {
        Console.WriteLine("Workflow runtime stopped");
    }
    

Compiling the Code

For information about compiling your code, see Compiling the Code.

In Task 2: Suspend, Terminate, and Resume Workflows, you 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

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