[This is preliminary documentation and is subject to change.]

Greetings (simulation derivation)

This is the third "Hello, world" example program. This class runs a simple simulation that prints a greeting message to standard output (e.g. the console). In this example, the main class is derived from the Simulation class. The SayHello method, which prints the greeting message, is used as the delegate.

The complete code for this example is shown below and is also available as greetings3.cs in the examples\greetings directory.

using System;
using System.Collections.Generic;
using React;

public class Greetings : React.Simulation
{
    private Greetings()
    {
        Process process = new Process(this, SayHello);
        process.Activate(this);
    }

    private IEnumerator<Task> SayHello(Process process, object data)
    {
        Console.WriteLine("Greetings3 says, Hello there ...");
        yield break;
    }

    public static void Main(string [] args)
    {
        Greetings greetings = new Greetings();
        greetings.Run();
    }
}

The Explanation

Start by including references to some of the required namespaces. The System.Collections.Generic namespace is required for the IEnumerator<Task> class. The React namespace is required for the Simulation, Task, and Process classes.

using System;
using System.Collections.Generic;
using React;

The Greetings class is derived from React.Simulation and contains a private constructor to prevent other classes from instantiating it. The constructor creates a Process that serves as the generator task. The Process delegates its actual functionality to the SayHello method.

public class Greetings
{
    private Greetings()
    {
        Process process = new Process(this, SayHello);
        process.Activate(this);
    }

          .
          .
          .
}

The private SayHello method defines the functionality of the Process that will be created in the constructor. SayHello is the delegate used by the Process to yield its Tasks. Of course, in this case, all the Process will do is print a message and exit -- no Tasks are returned by SayHello.

The yield break; statement must be included or SayHello will not be a valid .NET iterator.

private IEnumerator<Task> SayHello(Process process, object data)
{
    Console.WriteLine("Greetings3 says, Hello there ...");
    yield break;
}

Finally, the Main method instantiates a Greetings object and starts it running. No generator task is passed to the Run method because the generator was created and activated in the constructor. When the generator task is executed, the SayHello method will get called and the greetings message printed to the console.

Greetings greetings = new Greetings();
greetings.Run();