Observable.Window<TSource> 方法 (IObservable<TSource>、TimeSpan、TimeSpan、IScheduler)

将可观测序列的每个元素投影到基于计时信息生成的零个或多个窗口。

Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (

语法

'Declaration
<ExtensionAttribute> _
Public Shared Function Window(Of TSource) ( _
    source As IObservable(Of TSource), _
    timeSpan As TimeSpan, _
    timeShift As TimeSpan, _
    scheduler As IScheduler _
) As IObservable(Of IObservable(Of TSource))
'Usage
Dim source As IObservable(Of TSource)
Dim timeSpan As TimeSpan
Dim timeShift As TimeSpan
Dim scheduler As IScheduler
Dim returnValue As IObservable(Of IObservable(Of TSource))

returnValue = source.Window(timeSpan, _
    timeShift, scheduler)
public static IObservable<IObservable<TSource>> Window<TSource>(
    this IObservable<TSource> source,
    TimeSpan timeSpan,
    TimeSpan timeShift,
    IScheduler scheduler
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IObservable<IObservable<TSource>^>^ Window(
    IObservable<TSource>^ source, 
    TimeSpan timeSpan, 
    TimeSpan timeShift, 
    IScheduler^ scheduler
)
static member Window : 
        source:IObservable<'TSource> * 
        timeSpan:TimeSpan * 
        timeShift:TimeSpan * 
        scheduler:IScheduler -> IObservable<IObservable<'TSource>> 
JScript does not support generic types and methods.

类型参数

  • TSource
    源的类型。

parameters

返回值

类型: System.IObservable<IObservable<TSource>>
可观察的窗口序列。

使用说明

在 Visual Basic 和 C# 中,可以将此方法作为 IObservable<TSource> 类型的任何对象的实例方法调用。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅

备注

Window 运算符将源序列分解为缓冲子集,如序列的窗口视图。 timeSpan 参数通过在该时间跨度期间保持窗口打开来控制每个窗口缓冲区中放置的项数。 timeShift 参数指示从上一窗口开始的时间跨度,该时间跨度必须在新窗口打开之前完成。 这会根据该时间跨度的持续时间将视图移动到序列中。 计划程序参数控制运行与 timeSpan 和 timeShift 参数关联的计时器的位置。

示例

在此示例中,Window 运算符用于每秒从 Interval 运算符观察一次整数序列。 通过窗口查看每个整数序列。 每个窗口将打开 2.5 秒,然后关闭。 timeShift 参数设置为 5 秒 。 这意味着,每打开一个前一个窗口后,每 5 秒就会打开一个新窗口。 最终结果是,窗口打开 2.5 秒,然后关闭 2.5 秒。 因此,序列将包含两个整数,从第 5 个整数开始,从 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 sec but  ***//
      //*** this sequence is broken up by the Window operator into subsets like a windowed         ***//
      //*** view of the sequence.                                                                  ***//
      //***                                                                                        ***//
      //*** The timeSpan parameter controls how many items are placed in each window buffer by     ***//
      //*** keeping the window open for the duration of the time span.                             ***//
      //***                                                                                        ***//
      //*** The timeShift parameter indicates the time span which must complete before a new       ***//
      //*** window opens. This shifts the view into the sequence based on the duration of the time ***//
      //*** span.                                                                                  ***//
      //***                                                                                        ***//
      //*** The ThreadPool scheduler is used to run the timers on a .NET thread pool thread. This  ***//
      //*** prevents the main thread from being blocked so pressing enter can exit the example.    ***//
      //***                                                                                        ***//
      //*** In this example each window will be open for 2.5 seconds. This will allow each window  ***//
      //*** to hold some items from the sequence starting with the first item (0). Then the        ***//
      //*** timeShift parameter shifts the next window opening by 5 seconds from the beginning of  ***//
      //*** the previous window. The result is that a window is open for 2.5 seconds then closed   ***//
      //*** for 2.5 seconds.                                                                       ***//
      //**********************************************************************************************//

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

      TimeSpan timeSpan = TimeSpan.FromSeconds(2.5);
      TimeSpan timeShift = TimeSpan.FromSeconds(5);
      var seqWindowed = mainSequence.Window(timeSpan, timeShift, Scheduler.ThreadPool);


      //*********************************************************************************************//
      //*** A subscription to seqWindowed will provide a new IObservable<long> for some items in  ***//
      //*** the main sequence starting with the first item. Then we will receive a new observable ***//
      //*** for every window.                                                                     ***//
      //***                                                                                       ***//
      //*** Create a subscription to each window into the main sequence and list the values.      ***//
      //*********************************************************************************************//

      Console.WriteLine("Creating 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();
    }
  }
}

示例代码生成了以下输出。

Creating the subscription. Press ENTER to exit...


A new window into the main sequence has been opened

Integer : 0
Integer : 1

A new window into the main sequence has been opened

Integer : 5
Integer : 6

A new window into the main sequence has been opened

Integer : 10
Integer : 11

A new window into the main sequence has been opened

Integer : 15
Integer : 16

A new window into the main sequence has been opened

Integer : 20
Integer : 21

另请参阅

参考

可观测类

窗口重载

System.Reactive.Linq 命名空间