將可觀察序列的每個元素投影到連續的非重迭視窗。
Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (
Syntax
'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 運算子每六秒產生的可觀察整數序列所控制。 由於主要可觀察序列每隔幾秒都會產生一個專案,因此每個視窗都會有六個專案。 範例程式碼會將整數的每個視窗寫入主控台視窗,以及顯示新視窗的時間戳記每隔六秒開啟一次。
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