Observable.Amb<TSource, metoda (IObservable<TSource>, IObservable<TSource>>)
Propaguje obserwowaną sekwencję, która reaguje najpierw z określoną pierwszą i drugą sekwencją.
Przestrzeń nazw:System.Reactive.Linq
Zestawu: System.Reaktywny (w System.Reactive.dll)
Składnia
'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 źródła.
Parametry
- Pierwszym
Typ: System.IObservable<TSource>
Pierwsza obserwowana sekwencja.
- sekunda
Typ: System.IObservable<TSource>
Druga obserwowana sekwencja.
Wartość zwracana
Typ: System.IObservable<TSource>
Obserwowana sekwencja, która odpowiedziała najpierw z zestawu podanych sekwencji.
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
Nazwa operatora Amb jest krótka dla "niejednoznacznego". Operator Amb przyjmuje co najmniej dwie sekwencje i zwraca pierwszą sekwencję do odpowiedzi. Operator Amb używa przetwarzania równoległego do wykrywania, która sekwencja zwraca pierwszy element.
Amb może być wywoływana jako metoda rozszerzenia, jak pokazano w przykładzie kodu w tym temacie lub może być wywoływana jako metoda statyczna. Na podstawie przykładu kodu podanego w tym temacie poniższy fragment kodu pokazuje, jak wywołać metodę statyczną 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();
}
Protokół Amb można również wywołać jako metodę rozszerzenia dla więcej niż dwóch sekwencji. Aby użyć tego podejścia, utwórz sekwencję sekwencji. Poniższy fragment kodu pokazuje to.
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();
}
Przykłady
W poniższym przykładzie pokazano operator Amb, stosując go z dwiema sekwencjami liczb całkowitych. Dostarczanie liczb całkowitych w pierwszej sekwencji jest opóźnione o trzy sekundy. Dostarczanie liczb całkowitych w drugiej sekwencji jest opóźnione tylko o dwie sekundy. Druga sekwencja odpowiada najpierw i jest wynikiem operatora Amb, jak pokazano.
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();
}
}
}
}
Dane wyjściowe z przykładowego kodu są następujące.
Press Enter to exit...
4
5
6