Observable.Window<TSource、TWindowClosing> メソッド (IObservable<TSource>、Func<IObservable<TWindowClosing>>)

監視可能なシーケンスの各要素を連続する重複しないウィンドウに投影します。

Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'Declaration
<ExtensionAttribute> _
Public Shared Function Window(Of TSource, TWindowClosing) ( _
    source As IObservable(Of TSource), _
    windowClosingSelector As Func(Of IObservable(Of TWindowClosing)) _
) As IObservable(Of IObservable(Of TSource))
'Usage
Dim source As IObservable(Of TSource)
Dim windowClosingSelector As Func(Of IObservable(Of TWindowClosing))
Dim returnValue As IObservable(Of IObservable(Of TSource))

returnValue = source.Window(windowClosingSelector)
public static IObservable<IObservable<TSource>> Window<TSource, TWindowClosing>(
    this IObservable<TSource> source,
    Func<IObservable<TWindowClosing>> windowClosingSelector
)
[ExtensionAttribute]
public:
generic<typename TSource, typename TWindowClosing>
static IObservable<IObservable<TSource>^>^ Window(
    IObservable<TSource>^ source, 
    Func<IObservable<TWindowClosing>^>^ windowClosingSelector
)
static member Window : 
        source:IObservable<'TSource> * 
        windowClosingSelector:Func<IObservable<'TWindowClosing>> -> IObservable<IObservable<'TSource>> 
JScript does not support generic types and methods.

型パラメーター

  • TSource
    ソースの種類。
  • TWindowClosing
    ウィンドウを閉じる種類。

パラメーター

  • source
    種類: System.IObservable<TSource>
    ウィンドウを生成するソース シーケンス。
  • windowClosingSelector
    種類: System.Func<IObservable<TWindowClosing>>
    生成されたウィンドウの境界を定義するために呼び出される関数。

戻り値

種類: System.IObservable<IObservable<TSource>>
ウィンドウの監視可能なシーケンス。

使用上の注意

Visual Basic と C# では、 IObservable<TSource> 型の任意のオブジェクトのインスタンス メソッドとしてこのメソッドを呼び出すことができます。 インスタンス メソッド構文を使用してこのメソッドを呼び出す場合は、最初のパラメーターを省略します。 詳細については、」または」を参照してください。

解説

Window 演算子は、監視可能なシーケンスを連続する重複しないウィンドウに分割します。 現在のウィンドウの末尾と次のウィンドウの開始は、演算子に入力パラメーターとして渡される windowClosingSelect 関数の結果である監視可能なシーケンスによって制御されます。 演算子を使用して、一連のイベントをウィンドウにグループ化できます。 たとえば、トランザクションの状態は、観察されるメインシーケンスである可能性があります。 これらの状態には、準備、準備、アクティブ、コミット/中止が含まれます。 メイン シーケンスには、これらの状態がすべてその順序で発生している状態が含まれる場合があります。 windowClosingSelect 関数は、Committed 状態または Abort 状態の値のみを生成する監視可能なシーケンスを返す可能性があります。 これにより、特定のトランザクションのトランザクション イベントを表すウィンドウが閉じます。

次の簡単な例では、整数のシーケンスを連続する重複しないウィンドウに分割します。 現在のウィンドウの末尾と次のウィンドウの開始は、Interval 演算子によって 6 秒ごとに生成される監視可能な整数シーケンスによって制御されます。 監視可能なシーケンスメインは数秒ごとに項目を生成するため、各ウィンドウには 6 つの項目が含まれます。 このコード例では、新しいウィンドウが 6 秒ごとに開かれることを示すタイムスタンプと共に、整数の各ウィンドウをコンソール ウィンドウに書き込みます。

using System;
using System.Reactive.Linq;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*********************************************************************************************//
      //*** The mainSequence produces a new long integer from the Interval operator every sec but ***//
      //*** this sequence is broken up by the Window operator into subsets like a windowed        ***//
      //*** view of the sequence. The time when each window stops and the next window starts is   ***//
      //*** controlled by the IObservable<TWindowClosing> named seqWindowControl. It is returned  ***//
      //*** by the lambda expression which is passed to the Window operator. In this case it      ***//
      //**  returns another IObservable<long> generated by the Interval operator. So whenever     ***//
      //*** seqWindowControl produces a item, the current window into the mainSequence stops and  ***//
      //*** a new window starts.                                                                  ***//
      //*********************************************************************************************//

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

      var seqWindowed = mainSequence.Window(() => 
      {
        var seqWindowControl = Observable.Interval(TimeSpan.FromSeconds(6));
        return seqWindowControl;
      });


      //*********************************************************************************************//
      //*** A subscription to seqWindowed will provide a new IObservable<long> every 6 secs.      ***//
      //***                                                                                       ***//
      //*** Create a subscription to each window into the main sequence and list the values along ***//
      //*** with the time the window was opened and the previous window was closed.               ***//
      //*********************************************************************************************//
      
      seqWindowed.Subscribe(seqWindow => 
      {
        Console.WriteLine("\nA new window into the main sequence has opened: {0}\n",DateTime.Now.ToString());
        seqWindow.Subscribe(x =>
        {
          Console.WriteLine("Integer : {0}", x);
        });
      });

      Console.ReadLine();
    }
  }
}

次の出力は、コード例によって生成されました。

A new window into the main sequence has opened: 6/1/2011 8:48:43 PM

Integer : 0
Integer : 1
Integer : 2
Integer : 3
Integer : 4
Integer : 5

A new window into the main sequence has opened: 6/1/2011 8:48:49 PM

Integer : 6
Integer : 7
Integer : 8
Integer : 9
Integer : 10
Integer : 11

A new window into the main sequence has opened: 6/1/2011 8:48:55 PM

Integer : 12
Integer : 13
Integer : 14
Integer : 15
Integer : 16
Integer : 17

A new window into the main sequence has opened: 6/1/2011 8:49:02 PM

Integer : 18
Integer : 19
Integer : 20
Integer : 21
Integer : 22
Integer : 23

参照

リファレンス

Observable クラス

ウィンドウ オーバーロード

System.Reactive.Linq 名前空間