Observable.Amb<TSource> メソッド (IObservable<TSource>、IObservable<TSource>)
指定した 1 番目と 2 番目のシーケンスと最初に反応する観測可能なシーケンスを伝達します。
Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)
構文
'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>
2 番目の観測可能なシーケンス。
戻り値
種類: System.IObservable<TSource>
指定されたシーケンスのセットから最初に応答した監視可能なシーケンス。
使用上の注意
Visual Basic および C# では、 IObservable<TSource> 型の任意のオブジェクトでインスタンス メソッドとしてこのメソッドを呼び出すことができます。 インスタンス メソッド構文を使用してこのメソッドを呼び出す場合は、最初のパラメーターを省略します。 詳細については、」または」を参照してください。
解説
Amb 演算子の名前は、"あいまい" の短いです。 Amb 演算子は、2 つ以上のシーケンスを受け取り、応答する最初のシーケンスを返します。 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 は、2 つ以上のシーケンスの拡張メソッドとして呼び出すこともできます。 この方法を使用するには、シーケンスのシーケンスを作成します。 次のコード スニペットは、これを示しています。
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();
}
例
次の例では、2 つの整数シーケンスを使用して Amb 演算子を適用する方法を示します。 最初のシーケンスの整数の配信は、3 秒遅れます。 2 番目のシーケンスの整数の配信は、2 秒だけ遅れます。 したがって、2 番目のシーケンスは最初に応答し、次に示すように 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