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 the execution of individual workflows. Specifically, this task demonstrates how to suspend, resume, and terminate workflows by creating event handlers for specific workflow events.

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.

Creating Event Handlers

Follow these steps to create event handlers for the WorkflowSuspended, WorkflowResumed, and WorkflowTerminated events.

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.

    Visual Basic
    AddHandler workflowRuntime.WorkflowSuspended, _
        AddressOf workflowRuntime_WorkflowSuspended
    C#
    workflowRuntime.WorkflowSuspended += new 
        EventHandler<WorkflowSuspendedEventArgs>
        (workflowRuntime_WorkflowSuspended);
  2. Create an event handler for the WorkflowResumed event that is defined in the workflowRuntime object.

    Visual Basic
    AddHandler workflowRuntime.WorkflowResumed, _
        AddressOf workflowRuntime_WorkflowResumed
    C#
    workflowRuntime.WorkflowResumed += new 
        EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowResumed);
  3. Create an event handler for the WorkflowTerminated event that is defined in the workflowRuntime object.

    Visual Basic
    AddHandler workflowRuntime.WorkflowTerminated, _
        AddressOf workflowRuntime_WorkflowTerminated
    C#
    workflowRuntime.WorkflowTerminated += new 
        EventHandler<WorkflowTerminatedEventArgs>
        (workflowRuntime_WorkflowTerminated);

Creating Event Handler Methods

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.

    Visual Basic
    Private Shared Sub workflowRuntime_WorkflowSuspended(ByVal sender As Object, _
        ByVal e As WorkflowSuspendedEventArgs)
        Console.WriteLine("Workflow suspended: {0}", e.Error)
        e.WorkflowInstance.Resume()
    End Sub
    C#
    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.

    Visual Basic
    Private Shared Sub workflowRuntime_WorkflowResumed(ByVal sender As Object, _
        ByVal e As WorkflowEventArgs)
        Console.WriteLine("Workflow resumed")
    End Sub
    C#
    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 executing when you receive the WorkflowTerminated event, call the Set method that is defined in the waitHandle object to close your application properly.

    Visual Basic
    Private Shared Sub workflowRuntime_WorkflowTerminated(ByVal sender As Object, _
        ByVal e As WorkflowTerminatedEventArgs)
        Console.WriteLine("Workflow Terminated : {0}", e.Exception.Message)
        waitHandle.Set()
    End Sub
    C#
    static void workflowRuntime_WorkflowTerminated(object sender, 
        WorkflowTerminatedEventArgs e)
    {
        Console.WriteLine("Workflow Terminated : {0}", e.Exception.Message);
        waitHandle.Set();
    }

Suspending and Terminating the Workflow

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.

    NoteNote

    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. You will see that 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 executing.

    Visual Basic
    workflowInstance.Start
    workflowInstance.Suspend("Suspending workflow")

    C#
    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. You will see that 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.

    NoteImportant

    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.

    Visual Basic
    workflowInstance.Start
    workflowInstance.Terminate("Terminating workflow")

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

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 Exercise 4: Use Runtime Services, you will explore two different ways to add services to the Windows Workflow Foundation runtime engine.

See Also

Footer image
Send comments about this topic to Microsoft.
Page view tracker