Observable.window<TSource, metoda (IObservable<TSource>>, TimeSpan, TimeSpan, IScheduler)
Projektuje każdy element widocznej sekwencji na zero lub więcej okien, które są generowane na podstawie informacji o chronometrażu.
Przestrzeń nazw:System.Reactive.Linq
Zestawu: System.Reaktywny (w System.Reactive.dll)
Składnia
'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.
Parametry typu
- Tsource
Typ źródła.
Parametry
- source
Typ: System.IObservable<TSource>
Sekwencja źródłowa do tworzenia okien.
- Timespan
Typ: System.TimeSpan
Długość każdego okna.
- Timeshift
Typ: System.TimeSpan
Interwał między tworzeniem kolejnych okien.
- scheduler
Typ: System.Reactive.Concurrency.IScheduler
Harmonogram uruchamiania czasomierzy okien.
Wartość zwracana
Typ: System.IObservable IObservable<<TSource>>
Zauważalna sekwencja okien.
Uwaga dotycząca użycia
W języku Visual Basic i C#można wywołać tę metodę jako metodę wystąpienia w dowolnym obiekcie typu IObservable<TSource>. Gdy w celu wywołania tej metody jest używana składnia metody wystąpienia, należy pominąć pierwszy parametr. Aby uzyskać więcej informacji, zobacz lub .
Uwagi
Operator Okna dzieli sekwencję źródłową na buforowane podzestawy, takie jak widok okna sekwencji. Parametr timeSpan steruje liczbą elementów umieszczonych w każdym buforze okien, utrzymując otwarte okno przez czas trwania tego przedziału czasu. Parametr timeShift wskazuje przedział czasu od początku poprzedniego okna, który musi zostać ukończony przed otwarciem nowego okna. Spowoduje to przesunięcie widoku do sekwencji na podstawie czasu trwania tego przedziału czasu. Parametr harmonogramu steruje miejscem uruchamiania czasomierzy skojarzonych z parametrami timeSpan i timeShift.
Przykłady
W tym przykładzie operator Okna służy do obserwowania sekwencji całkowitych z operatora Interwał co sekundę. Każda sekwencja całkowita jest widoczna za pośrednictwem okna. Każde okno będzie otwarte przez 2,5 sekundy, a następnie zamknięte. Parametr timeShift jest ustawiony na 5 sekund. Oznacza to, że nowe okno będzie otwierane co 5 sekund od czasu otwarcia każdego poprzedniego okna. Wynikiem końcowym jest otwarcie okna przez 2,5 sekundy, a następnie zamknięte przez 2,5 sekundy. Dlatego sekwencje będą zawierać dwie liczby całkowite rozpoczynające się od każdej 5 liczby całkowitej rozpoczynającej się od 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();
}
}
}
Następujące dane wyjściowe zostały wygenerowane przez przykładowy kod.
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