Delen via


Task 1: Add and Initialize the Workflow Members

Download sample

In this task, you add a PolicyActivity activity and create a RuleSet that implements a binary search algorithm that is used to detect the random number generated by the workflow. This is done by using the rules chaining feature of Windows Workflow Foundation (WF).

Note

Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start on this exercise by opening the sample project and proceeding to the steps in the following section.

Add required workflow members

First, member variables are added to the workflow to support the binary search.

To add the workflow members

  1. Open Workflow1 in code view (right click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Code).

  2. Add the following member declarations to the Workflow1 class.

    public int ComputerGuess;
    public int ComputerTurns;
    public int min;
    public int max;
    

Initialize workflow members

Modify the GameStarted method in the Workflow1 class to initialize min and max to the upper and lower bound for the binary search.

To initialize min and max

  1. Add the following statements to the GameStarted method.

    min = 1;
    max = MaxNumber;
    

    The final GameStarted method appears as follows:

    private void GameStarted(object sender, EventArgs e)
    {
        // Generate the Random number.
        TargetNumber = new Random().Next(1, MaxNumber + 1);
    
        // Initialize the game variables.
        Turns = 0;
        Guess = 0;
    
        // Prompt the user
        Console.WriteLine("Please guess a number from 1 to {0}", MaxNumber);
    
        // Initialize the boundaries for the
        // binary search.
        min = 1;
        max = MaxNumber;
    }
    

Create a method to report the binary search progress

One of the rules created in the next exercise invokes a method to display the progress of the binary search. For the rule to be successfully created, the method must exist before the rule is created.

To create the progress method

  1. Create a new method in the Workflow1 class called DisplayComputerGuess that contains the following statements.

    private void DisplayComputerGuess()
    {
        Console.WriteLine("Computer guess: {0}", ComputerGuess);
    }
    

Build the Application

Choose Build Solution from the Build menu to verify that the application builds correctly.

In Task 2: Use Forward Chaining of Rules the PolicyActivity activity and rules are added to implement the binary search.

See Also

Reference

SequentialWorkflowActivity
WorkflowTerminated
WorkflowCompleted
StartRuntime
WorkflowCompletedEventArgs
WorkflowTerminatedEventArgs

Concepts

Creating a Workflow Host Application

Other Resources

Task 2: Use Forward Chaining of Rules
Sequential Workflow With Parameters

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