Compartilhar via


Canceling a Workflow Sample

Download sample

This sample demonstrates how to exit a workflow that is waiting for an external event that will never transpire. The workflow is an expense reporting sample that submits an expense report and waits for approval. However, the sample was created so that the approval event never arrives.

The host console application implements a cancel workflow feature that causes the non-terminating workflow to exit. The difference between this feature and calling the Terminate method to terminate a workflow is that termination is a forced and abnormal exit that does not run any exception or compensation handlers. The method employed by this sample allows exception handlers and compensation handlers to unwind and eventually terminate the workflow.

To do this, the host application queries the runtime to discover what activity is blocking the workflow. The queue name of the workflow is returned in the GetWorkflowQueueData method. An exception that is not handled by the workflow is delivered to this queue via the EnqueueItem method.

Note

In this sample, the idled event is used to determine whether the workflow is paused waiting for an event, but a more robust application should confirm that the workflow is not idled for other reasons.

The output of the sample will appear as follows:

Canceling a Workflow Sample Output

To build the sample

  1. Download the sample by clicking Download Sample.

    This extracts the sample project to your local hard disk.

  2. Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.

  3. Go to the source directory of the sample.

  4. At the command prompt, type MSBUILD <Solution file name>.

To run the sample

  1. In the SDK Command Prompt window, run the .exe file in the CancelWorkflow\bin\debug folder (or the CancelWorkflow\bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.

Database Configuration

The SQL services that are installed by Windows Workflow Foundation use Microsoft SQL Server to store information. You can use SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE) for these tasks.

Windows Workflow Foundation Setup does not install the databases that are required by these services; however, Setup installs the SQL scripts for creating and configuring the databases for these services.

The following steps describe how to create and configure the databases that are used by services in this sample.

To create and configure the SQL Server database

  1. Using Microsoft SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE), create a new database named SqlPersistenceService using the SQL query statement:

    CREATE DATABASE SqlPersistenceService

    Note   When using both the SqlTrackingService and the SqlWorkflowPersistenceService in a workflow application, it is recommended that you use a single database for both persistence and tracking.

  2. In the SQL Query Analyzer workspace, select the database that you created in step 1 in the list of available databases.

  3. On the File menu, click Open, and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistenceService_Schema.

  4. Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service tables.

  5. On the File menu, click Open, and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistenceService_Logic.

  6. Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service stored procedures.

See Also

Reference

GetWorkflowQueueData
EnqueueItem
WorkflowQueueInfo
TerminateActivity
Terminate

Other Resources

Hosting Samples
Hosting the Windows Workflow Foundation Runtime

© 2007 Microsoft Corporation. All rights reserved.