Observable.Amb < TSource > 方法 (IObservable TSource > 、IObservable << TSource >)
傳播可觀察序列,此序列會先回應指定的第一個和第二個序列。
Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (
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.
類型參數
- TSource
來源的類型。
參數
- first
類型:System.IObservable< TSource>
第一個可觀察序列。
- second
類型:System.IObservable< TSource>
第二個可觀察序列。
傳回值
類型:System.IObservable< TSource>
可觀察的序列,會先從指定序列集合中回應。
使用注意事項
在 Visual Basic 和 C# 中,您可以在IObservable< TSource > 類型的任何物件上呼叫這個方法作為實例方法。 使用執行個體方法語法呼叫這個方法時,請省略第一個參數。 如需詳細資訊,請參閱 或 。
備註
Amb 運算子的名稱簡短表示「模棱兩可」。 Amb 運算子會接受兩個或多個序列,並傳回第一個序列來回應。 Amb 運算子會使用平行處理來偵測哪一個序列會產生第一個專案。
Amb 可以呼叫為擴充方法,如本主題的程式碼範例所示,也可以呼叫為靜態方法。 根據本主題所提供的程式碼範例,下列程式碼片段示範如何呼叫 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 也可以呼叫為兩個以上序列的擴充方法。 若要使用此方法,請建立序列。 下列程式碼片段示範這一點。
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();
}
範例
下列範例示範 Amb 運算子,方法是使用兩個整數序列來套用它。 第一個序列中的整數傳遞延遲三秒。 第二個序列中的整數傳遞只會延遲兩秒。 因此,第二個序列會先回應,而且是 Amb 運算子的結果,如下所示。
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();
}
}
}
}
範例程式碼的輸出如下所示。
Press Enter to exit...
4
5
6