Synchronized Sample

Download sample

This sample demonstrates how to author a workflow that synchronizes the execution of groups of activities. This is useful, for example, to synchronize access to a shared resource.

Synchronized data access is an important consideration for composite activities such as ParallelActivity, ReplicatorActivity, and ConditionedActivityGroup that have parallel execution paths.

In this sample, the workflow has a ParallelActivity that contains two branches. Each branch contains a SynchronizationScopeActivity activity. Two CodeActivity activities in each branch try to increment and decrement a workflow instance count. One code branch increments (count++) while the other code branch decrements (count--).

The two SynchronizedScope instances will be synchronized because they share a common value in their SynchronizationHandles property, "syncMutex".This ensures that after the first SynchronizedScope activity begins executing it will be allowed to complete before the other SynchronizedScope can begin. While this will not change the final value of the "count" field in this simple example, it does impact the intermediate values. To see the behavior without synchronization, change the value of the SynchronizationHandles property on one of the SynchronizedScopes, so that the two SynchronizedScopes are no longer synchronized.

When you build and run the sample, your output will appear similar to the following:

Output from Synchronized sample

To build the sample

  1. Download the sample by clicking Download Sample in this topic.

    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 Synchronized\bin\debug folder (or the Synchronized\bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.

See Also

Reference

ParallelActivity
ReplicatorActivity
ConditionedActivityGroup
SynchronizationScopeActivity

Other Resources

Using the ParallelActivity Activity
Using the ReplicatorActivity Activity
Using the ConditionedActivityGroup Activity
Using the SynchronizationScopeActivity Activity
Activities Samples
Windows Workflow Foundation Samples

© 2007 Microsoft Corporation. All rights reserved.