Observable.Amb<TSource> – metoda (IObservable<TSource>, IObservable<TSource>)
Šíří pozorovatelnou sekvenci, která reaguje jako první se zadanou první a druhou sekvencí.
Obor názvů:System.Reactive.Linq
Sestavení: System.Reactive (v System.Reactive.dll)
Syntax
'Declaration
<ExtensionAttribute> _
Public Shared Function Amb(Of TSource) ( _
first As IObservable(Of TSource), _
second As IObservable(Of TSource) _
) As IObservable(Of TSource)
'Usage
Dim first As IObservable(Of TSource)
Dim second As IObservable(Of TSource)
Dim returnValue As IObservable(Of TSource)
returnValue = first.Amb(second)
public static IObservable<TSource> Amb<TSource>(
this IObservable<TSource> first,
IObservable<TSource> second
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IObservable<TSource>^ Amb(
IObservable<TSource>^ first,
IObservable<TSource>^ second
)
static member Amb :
first:IObservable<'TSource> *
second:IObservable<'TSource> -> IObservable<'TSource>
JScript does not support generic types and methods.
Parametry typu
- Tsource
Typ zdroje.
Parametry
- První
Typ: System.IObservable<TSource>
První pozorovatelná sekvence.
- second
Typ: System.IObservable<TSource>
Druhá pozorovatelná posloupnost.
Návratová hodnota
Typ: System.IObservable<TSource>
Pozorovatelná sekvence, která reagovala jako první ze sady daných sekvencí.
Poznámka k využití
V jazyce Visual Basic a C# můžete tuto metodu volat jako metodu instance u libovolného objektu typu IObservable<TSource>. Pokud k volání této metody použijete syntaxi metody instance, vynechejte první parametr. Další informace naleznete v tématech a .
Poznámky
Název operátoru Amb je zkratka pro "nejednoznačný". Operátor Amb přijímá dvě nebo více sekvencí a vrací první sekvenci, která má reagovat. Operátor Amb používá paralelní zpracování ke zjištění, která sekvence vynese první položku.
Amb lze volat jako rozšiřující metodu, jak je znázorněno v příkladu kódu v tomto tématu, nebo může být volána jako statická metoda. Na základě příkladu kódu uvedené v tomto tématu následující fragment kódu ukazuje, jak volat statickou metodu Amb.
int[] sequence1 = { 1, 2, 3 };
int[] sequence2 = { 4, 5, 6 };
int[] sequence3 = { 7, 8, 9 };
//*** The first item in the sequence1 event stream is delayed by 3 seconds. ***//
IObservable<int> firstSource = sequence1.ToObservable().Delay(TimeSpan.FromSeconds(3));
//*** The first event in the sequence2 event stream is only delayed by 2 seconds. ***//
IObservable<int> secondSource = sequence2.ToObservable().Delay(TimeSpan.FromSeconds(2));
//*** The first event in the sequence3 event stream is only delayed by 1 second. ***//
IObservable<int> thirdSource = sequence3.ToObservable().Delay(TimeSpan.FromSeconds(1));
//*****************************************************************************************//
//*** ***//
//*** The Amb operator will simply return the observable sequence which responds first. ***//
//*** The other sequence will be ignored. ***//
//*** ***//
//*** In this example "thirdSource", which contains sequence3, will respond before ***//
//*** "firstSource" and "secondSource". So "thirdSource" will be the observable ***//
//*** sequence returned from the Amb operator. It will be subscribed to and written ***//
//*** to the console. ***//
//*** ***//
//*****************************************************************************************//
//*** The static method allows the Amb operator to process more than two sequences ***//
using (IDisposable handle = Observable.Amb(firstSource, secondSource, thirdSource).Subscribe(value => Console.WriteLine(value)))
{
Console.WriteLine("\nPress ENTER to exit...\n");
Console.ReadLine();
}
Amb lze také volat jako rozšiřující metodu pro více než dvě sekvence. Pokud chcete tento přístup použít, vytvořte posloupnost sekvencí. Následující fragment kódu to ukazuje.
int[] sequence1 = { 1, 2, 3 };
int[] sequence2 = { 4, 5, 6 };
int[] sequence3 = { 7, 8, 9 };
//*** The first item in the sequence1 event stream is delayed by 3 seconds. ***//
IObservable<int> firstSource = sequence1.ToObservable().Delay(TimeSpan.FromSeconds(3));
//*** The first event in the sequence2 event stream is only delayed by 2 seconds. ***//
IObservable<int> secondSource = sequence2.ToObservable().Delay(TimeSpan.FromSeconds(2));
//*** The first event in the sequence3 event stream is only delayed by 1 second. ***//
IObservable<int> thirdSource = sequence3.ToObservable().Delay(TimeSpan.FromSeconds(1));
//*****************************************************************************************//
//*** ***//
//*** The Amb operator will simply return the observable sequence which responds first. ***//
//*** The other sequence will be ignored. ***//
//*** ***//
//*** In this example "thirdSource", which contains sequence3, will respond before ***//
//*** "firstSource" and "secondSource". So "thirdSource" will be the observable ***//
//*** sequence returned from the Amb operator. It will be subscribed to and written ***//
//*** to the console. ***//
//*** ***//
//*****************************************************************************************//
//*** Call the extension method on a sequence of any number of sequences. ***//
IObservable<int>[] sources = new[] { firstSource, secondSource, thirdSource };
using(IDisposable handle = sources.Amb().Subscribe(value => Console.WriteLine(value)))
{
Console.WriteLine("\nPress ENTER to exit...\n");
Console.ReadLine();
}
Příklady
Následující příklad ukazuje operátor Amb tím, že ho použije se dvěma sekvencemi celých čísel. Doručení celých čísel v první sekvenci je zpožděno o tři sekundy. Doručení celých čísel v druhé sekvenci je zpožděno pouze o dvě sekundy. Druhá sekvence tedy reaguje jako první a je výsledkem operátoru Amb, jak je znázorněno na obrázku.
using System;
using System.Reactive.Linq;
namespace Example
{
class Program
{
static void Main()
{
int[] sequence1 = { 1, 2, 3 };
int[] sequence2 = { 4, 5, 6 };
//*** The first event in observable sequence1 is delayed by 3 seconds. ***//
IObservable<int> firstSource = sequence1.ToObservable().Delay(TimeSpan.FromSeconds(3));
//*** The first event in observable sequence2 is only delayed by 2 seconds. ***//
IObservable<int> secondSource = sequence2.ToObservable().Delay(TimeSpan.FromSeconds(2));
//*****************************************************************************************//
//*** ***//
//*** The Amb operator will simply return the observable sequence which responds first. ***//
//*** The other sequence will be ignored. ***//
//*** ***//
//*** In this example "secondSource", which contains sequence2, will respond before ***//
//*** "firstSource". So "secondSource" will be the observable sequence returned from ***//
//*** the Amb operator. ***//
//*** ***//
//*****************************************************************************************//
using (IDisposable handle = firstSource.Amb(secondSource).Subscribe(value => Console.WriteLine(value)))
{
Console.WriteLine("Press Enter to exit...\n");
Console.ReadLine();
}
}
}
}
Výstup z ukázkového kódu je následující.
Press Enter to exit...
4
5
6