将可观测序列的每个元素投影到连续的非重叠窗口。
Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (
语法
'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
窗口关闭的类型。
parameters
- source
类型: System.IObservable<TSource>
要生成窗口的源序列。
- windowClosingSelector
类型: System.Func<IObservable<TWindowClosing>>
调用的函数,用于定义生成的窗口的边界。
返回值
类型: System.IObservable<IObservable<TSource>>
可观察的窗口序列。
使用说明
在 Visual Basic 和 C# 中,可以将此方法作为 IObservable<TSource> 类型的任何对象的实例方法调用。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅或。
备注
Window 运算符将可观测序列分解为连续的非重叠窗口。 当前窗口的结束和下一个窗口的开始由可观察序列控制,该序列是 windowClosingSelect 函数的结果,该函数作为输入参数传递给运算符。 运算符可用于将一组事件分组到一个窗口中。 例如,事务的状态可以是观察到的main序列。 这些状态可能包括:“正在准备”、“已准备”、“活动”和“已提交/已中止”。 main序列可以包含所有这些状态,因为它们按该顺序发生。 windowClosingSelect 函数可以返回一个可观测序列,该序列仅在“已提交”或“中止”状态上生成值。 这将关闭表示特定事务的事务事件的窗口。
示例
下面的简单示例将一系列整数分解为连续的非重叠窗口。 当前窗口的结束和下一个窗口的开始由间隔运算符每六秒生成的可观察整数序列控制。 由于main可观测序列每隔几秒钟生成一个项,因此每个窗口将有六个项。 示例代码将每个整数窗口写入控制台窗口,以及显示每六秒打开一个新窗口的时间戳。
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