다음을 통해 공유


Observable.Amb<TSource> 메서드(IObservable<TSource>, IObservable<TSource>)

지정된 첫 번째 시퀀스와 두 번째 시퀀스를 사용하여 먼저 반응하는 관찰 가능한 시퀀스를 전파합니다.

네임스페이스:System.Reactive.Linq
어셈블리: System.Reactive(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.

형식 매개 변수

  • TSource
    원본의 형식입니다.

매개 변수

  • first
    형식: System.IObservable<TSource>
    관찰 가능한 첫 번째 시퀀스입니다.
  • second
    형식: System.IObservable<TSource>
    관찰 가능한 두 번째 시퀀스입니다.

반환 값

형식: System.IObservable<TSource>
지정된 시퀀스 집합에서 먼저 응답한 관찰 가능한 시퀀스입니다.

사용 정보

Visual Basic 및 C#에서는 IObservable TSource> 형식의 모든 개체에서 이 메서드를 instance 메서드로 호출할 수 있습니다<. 인스턴스 메서드 구문을 사용하여 이 메서드를 호출할 경우에는 첫 번째 매개 변수를 생략합니다. 자세한 내용은 또는 를 참조하세요.

설명

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 연산자를 적용하는 방법을 보여 줍니다. 첫 번째 시퀀스의 정수 배달이 3초 지연됩니다. 두 번째 시퀀스의 정수 배달은 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

참고 항목

참조

관찰 가능한 클래스

Amb 오버로드

System.Reactive.Linq 네임스페이스