Observable.Amb<TSource> 方法 (IObservable<TSource>、IObservable<TSource>)

传播可观测序列,该序列首先使用指定的第一个和第二个序列做出反应。

Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (

语法

'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
    源的类型。

参数

返回值

类型: System.IObservable<TSource>
在给定序列集中首先做出响应的可观测序列。

使用说明

在 Visual Basic 和 C# 中,可以将此方法作为 IObservable<TSource> 类型的任何对象的实例方法调用。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅

备注

Amb 运算符的名称缩写为“ambiguous”。 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

另请参阅

参考

可观测类

Amb 重载

System.Reactive.Linq 命名空间