This project has moved. For the latest updates, please go here.

Creating a Reactive Lab

The following procedure illustrates how to add a new Console Lab to the Hands-on Labs project.
Note: You must install the Labs Framework to use this procedure.
  1. Add a new item.
  2. Select the Console Lab template.
  3. Start coding your lab.

Figure 1: Add a new item to the Reactive folder.

AddNewItem.png


Figure 2: Select the Console Lab template in the Add New Item dialog.

AddNewConsoleLab.png


Figure 3: Start coding in the Main method.

using System;
using System.ComponentModel;
using System.Reactive.Linq;
using Rxx.Labs.Properties;

namespace Rxx.Labs.Reactive
{
    [DisplayName("")]
    [Description("")]
    public sealed class MyFirstLab : BaseConsoleLab
    {
        protected override void Main()
        {
            // Prompt the user for input.
            int take;
            while (!int.TryParse(UserInput("How many items? "), out take)) { }

            // Show instructions to the user.
            TraceLine(Instructions.PressAnyKeyToCancel);

            // Initialize lab state and observables.
            var xs = Observable.Interval(TimeSpan.FromSeconds(1)).Take(take);

            // Subscribe the base ConsoleOutput method (without invoking it).
            using (xs.Subscribe(ConsoleOutput))
            {
                // Optionally allow the user to cancel the subscription 
                // by pressing a key.
                WaitForKey();
            }

            // Alternatively, use the ForEach method instead of Subscribe to block
            // until the observable completes.
            xs.ForEach(ConsoleOutput);
        }
    }
}

Last edited Feb 14, 2012 at 3:52 AM by davedev, version 5