Export (0) Print
Expand All

Observable.Window(TSource) Method (IObservable(TSource), Int32, Int32)

Projects each element of an observable sequence into zero or more windows which are produced based on element count information.

Namespace:  System.Reactive.Linq
Assembly:  System.Reactive (in System.Reactive.dll)

static member Window : 
        source:IObservable<'TSource> * 
        count:int * 
        skip:int -> IObservable<IObservable<'TSource>> 

Type Parameters

TSource

The type of source.

Parameters

source
Type: System.IObservable(TSource)
The source sequence to produce windows over.
count
Type: System.Int32
The length of each window.
skip
Type: System.Int32
The number of elements to skip between creation of consecutive windows.

Return Value

Type: System.IObservable(IObservable(TSource))
An observable sequence of windows.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IObservable(TSource). When you use instance method syntax to call this method, omit the first parameter. For more information, see http://msdn.microsoft.com/en-us/library/bb384936(v=vs.103).aspx or http://msdn.microsoft.com/en-us/library/bb383977(v=vs.103).aspx.

The Window operator can be used to break up a sequence into buffered subsets like a windowed view of the sequence. The count parameter controls how many items are placed in each window. The skip parameter controls when the next window starts by counting the items produced in the main sequence. When the specified number of items is skipped, a new window starts to buffer a subset of the sequence.

This example produces a main sequence of integers using the Interval operator. A new integer is produced every second. The main sequence is broken up by the Window operator into subsets like a windowed view of the integer sequence. Each window will contain a count of three items from the sequence starting with the first item (0). Then five items are skipped so we achieve windows into the integer sequence that contain three integers. Each window starts with every 5th integer beginning at 0.

using System;
using System.Reactive.Linq;
using System.Reactive.Concurrency;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //***********************************************************************************************//
      //*** The mainSequence produces a new long integer from the Interval operator every second    ***//
      //*** but this sequence is broken up by the Window operator into subsets like a windowed      ***//
      //*** view of the sequence. The count parameter controls how many items are placed in each    ***//
      //*** window. The skip parameter controls when the next window starts by counting the items   ***//
      //*** produced in the main sequence. When the the specified number of items are skipped, a    ***//
      //*** new window starts.                                                                      ***//
      //***                                                                                         ***//
      //*** In this example each window will contain a count of 3 items from the sequence starting  ***//
      //*** with the first item (0). 5 items are "skipped" to determine when the next window opens. ***//
      //*** So the result is that the integer sequences in the windows start with every 5th integer ***//
      //*** beginning at 0 and include 3 integers.                                                  ***//
      //***********************************************************************************************//

      var mainSequence = Observable.Interval(TimeSpan.FromSeconds(1));

      int count = 3;
      int skip = 5;
      var seqWindowed = mainSequence.Window(count, skip);


      //*********************************************************************************************//
      //*** A subscription to seqWindowed will provide a new IObservable<long> for every 5th item ***//
      //*** in the main sequence starting with the first item.                                    ***//
      //***                                                                                       ***//
      //*** Create a subscription to each window into the main sequence and list the value.       ***//
      //*********************************************************************************************//

      Console.WriteLine("\nCreating the subscription. Press ENTER to exit...\n");
      seqWindowed.Subscribe(seqWindow =>
      {
        Console.WriteLine("\nA new window into the main sequence has been opened\n");

        seqWindow.Subscribe(x =>
        {
          Console.WriteLine("Integer : {0}", x);
        });
      });

      Console.ReadLine();
    }
  }
}

The following output was generated with the example code.

Creating the subscription. Press ENTER to exit...


A new window into the main sequence has been opened

Integer : 0
Integer : 1
Integer : 2

A new window into the main sequence has been opened

Integer : 5
Integer : 6
Integer : 7

A new window into the main sequence has been opened

Integer : 10
Integer : 11
Integer : 12

A new window into the main sequence has been opened

Integer : 15
Integer : 16
Integer : 17
Show:
© 2014 Microsoft