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.
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. |
Creating Event Handlers
Follow these steps to create event handlers for the WorkflowSuspended, WorkflowResumed, and WorkflowTerminated events.
To create event handlers for the events
-
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.
AddHandler workflowRuntime.WorkflowSuspended, _
AddressOf workflowRuntime_WorkflowSuspended
workflowRuntime.WorkflowSuspended += new
EventHandler<WorkflowSuspendedEventArgs>
(workflowRuntime_WorkflowSuspended);
-
Create an event handler for the WorkflowResumed event that is defined in the workflowRuntime object.
AddHandler workflowRuntime.WorkflowResumed, _
AddressOf workflowRuntime_WorkflowResumed
workflowRuntime.WorkflowResumed += new
EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowResumed);
-
Create an event handler for the WorkflowTerminated event that is defined in the workflowRuntime object.
AddHandler workflowRuntime.WorkflowTerminated, _
AddressOf workflowRuntime_WorkflowTerminated
workflowRuntime.WorkflowTerminated += new
EventHandler<WorkflowTerminatedEventArgs>
(workflowRuntime_WorkflowTerminated);
Creating Event Handler Methods
To create event handler methods for the WorkflowSuspended, WorkflowResumed, and WorkflowTerminated events.
-
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.
-
In the workflowRuntime_WorkflowSuspended method, use WriteLine to display an informative message by using the Error property of the WorkflowSuspendedEventArgs parameter.
-
In the workflowRuntime_WorkflowSuspended method, resume the workflow by calling the Resume method from the WorkflowInstance object that is defined in the WorkflowSuspendedEventArgs parameter.
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
static void workflowRuntime_WorkflowSuspended(object sender,
WorkflowSuspendedEventArgs e)
{
Console.WriteLine("Workflow suspended: {0}", e.Error);
e.WorkflowInstance.Resume();
}
-
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.
-
In the workflowRuntime_WorkflowResumed method, use the WriteLine method to display an informative message.
Private Shared Sub workflowRuntime_WorkflowResumed(ByVal sender As Object, _
ByVal e As WorkflowEventArgs)
Console.WriteLine("Workflow resumed")
End Sub
static void workflowRuntime_WorkflowResumed(object sender, WorkflowEventArgs e)
{
Console.WriteLine("Workflow resumed");
}
-
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.
-
In the workflowRuntime_WorkflowTerminated method, use WriteLine to display an informative message by using the Message property of the WorkflowTerminatedEventArgs parameter.
-
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.
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
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
-
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. 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.
workflowInstance.Start
workflowInstance.Suspend("Suspending workflow")
workflowInstance.Start();
workflowInstance.Suspend("Suspending workflow");
-
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.
Important |
|---|
| 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
-
Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.
-
Go to the source directory of the tutorial.
-
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
about this topic to Microsoft.