Partager via


Task 2: Suspend, Terminate, and Resume Workflows

Download sample

The previous task, Task 1: Listen For and Process Events, demonstrated how to create event handlers to respond to events that are raised by the Windows Workflow Foundation runtime engine. This task shows you how to listen for and respond to events that are raised by running the individual workflows. Specifically, this task demonstrates how to suspend, resume, and terminate workflows by creating event handlers for specific workflow events.

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 event handlers for the events

  1. In the Main method of the Program class, after the WorkflowRuntime object event handlers, create an event handler for the WorkflowSuspended event that is defined in the workflowRuntime object.

    workflowRuntime.WorkflowSuspended += new
        EventHandler<WorkflowSuspendedEventArgs>
        (workflowRuntime_WorkflowSuspended);
    
  2. Create an event handler for the WorkflowResumed event that is defined in the workflowRuntime object.

    workflowRuntime.WorkflowResumed += new
        EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowResumed);
    
  3. Create an event handler for the WorkflowTerminated event that is defined in the workflowRuntime object.

    workflowRuntime.WorkflowTerminated += new
        EventHandler<WorkflowTerminatedEventArgs>
        (workflowRuntime_WorkflowTerminated);
    

To create event handler methods for the WorkflowSuspended, WorkflowResumed, and WorkflowTerminated events.

  1. In the Program class, create a new static method named workflowRuntime_WorkflowSuspended that takes an Object named sender and a WorkflowSuspendedEventArgs named e as parameters.

  2. In the workflowRuntime_WorkflowSuspended method, use WriteLine to display an informative message by using the Error property of the WorkflowSuspendedEventArgs parameter.

  3. In the workflowRuntime_WorkflowSuspended method, resume the workflow by calling the Resume method from the WorkflowInstance object that is defined in the WorkflowSuspendedEventArgs parameter.

    static void workflowRuntime_WorkflowSuspended(object sender,
        WorkflowSuspendedEventArgs e)
    {
        Console.WriteLine("Workflow suspended: {0}", e.Error);
        e.WorkflowInstance.Resume();
    }
    
  4. In the Program class, create a new static method named workflowRuntime_WorkflowResumed that takes a Object named sender and a WorkflowEventArgs named e as parameters.

  5. In the workflowRuntime_WorkflowResumed method, use the WriteLine method to display an informative message.

    static void workflowRuntime_WorkflowResumed(object sender, WorkflowEventArgs e)
    {
        Console.WriteLine("Workflow resumed");
    }
    
  6. In the Program class, create a new static method named workflowRuntime_WorkflowTerminated that takes an Object named sender and a WorkflowTerminatedEventArgs named e as parameters.

  7. In the workflowRuntime_WorkflowTerminated method, use WriteLine to display an informative message by using the Message property of the WorkflowTerminatedEventArgs parameter.

  8. Because the workflow has finished running when you receive the WorkflowTerminated event, call the Set method that is defined in the waitHandle object to close your application properly.

    static void workflowRuntime_WorkflowTerminated(object sender,
        WorkflowTerminatedEventArgs e)
    {
        Console.WriteLine("Workflow Terminated : {0}", e.Exception.Message);
        waitHandle.Set();
    }
    

To suspend and terminate the workflow

  1. In the Main method of the Program class, call the Suspend method that is defined in the WorkflowInstance object, which is created from calling CreateWorkflow.

    Note

    The Suspend method should be called after you start the workflow by using the Start method that is defined in WorkflowInstance.

    Build your project and run your application. After you receive the Started event, your application suspends the workflow. The runtime raises the WorkflowSuspended event and your handler for that event calls the Resume method to resume the workflow and continue running.

    workflowInstance.Start
    workflowInstance.Suspend("Suspending workflow")
    
    workflowInstance.Start();
    workflowInstance.Suspend("Suspending workflow");
    
  2. Comment out the code created in step 1 of this procedure, and call the Terminate method that is defined in the WorkflowInstance object.

    Build your project and run your application. After you receive the Started event, your application terminates the workflow. The runtime raises the WorkflowTerminated event and your application stops the Windows Workflow Foundation runtime engine and exits.

    Note

    Before proceeding to the next exercise in this tutorial, comment out the Terminate method call, and uncomment out the Suspend call that you created in step 1 of this procedure.

    workflowInstance.Start
    workflowInstance.Terminate("Terminating workflow")
    
    workflowInstance.Start();
    workflowInstance.Terminate("Terminating workflow");
    

Compiling the Code

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

In Exercise 4: Use Runtime Services, you can explore two different ways to add services to the Windows Workflow Foundation runtime engine.

See Also

Reference

Suspend
Resume
Terminate

Other Resources

Exercise 4: Use Runtime Services

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