다음을 통해 공유


Observable.Throttle<TSource> 메서드(IObservable<TSource>, TimeSpan, IScheduler)

지정된 원본 dueTime 및 스케줄러를 사용하여 적절한 시간 전에 다른 값이 뒤에 옵니다 관찰 가능한 시퀀스의 값을 무시합니다.

네임스페이스:System.Reactive.Linq
어셈블리: System.Reactive(System.Reactive.dll)

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function Throttle(Of TSource) ( _
    source As IObservable(Of TSource), _
    dueTime As TimeSpan, _
    scheduler As IScheduler _
) As IObservable(Of TSource)
'Usage
Dim source As IObservable(Of TSource)
Dim dueTime As TimeSpan
Dim scheduler As IScheduler
Dim returnValue As IObservable(Of TSource)

returnValue = source.Throttle(dueTime, _
    scheduler)
public static IObservable<TSource> Throttle<TSource>(
    this IObservable<TSource> source,
    TimeSpan dueTime,
    IScheduler scheduler
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IObservable<TSource>^ Throttle(
    IObservable<TSource>^ source, 
    TimeSpan dueTime, 
    IScheduler^ scheduler
)
static member Throttle : 
        source:IObservable<'TSource> * 
        dueTime:TimeSpan * 
        scheduler:IScheduler -> IObservable<'TSource> 
JScript does not support generic types and methods.

형식 매개 변수

  • TSource
    원본의 형식입니다.

매개 변수

  • dueTime
    형식: System.TimeSpan
    각 값에 대한 제한 기간의 기간입니다.

반환 값

형식: System.IObservable<TSource>
관찰 가능한 시퀀스의 값으로, 기한 전에 다른 값이 뒤따릅니다.

사용 정보

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

설명

Throttle 연산자는 시퀀스에서 항목을 버퍼링하고 dueTime 매개 변수로 지정된 시간 범위가 만료될 때까지 기다립니다. 시간 범위가 만료되기 전에 시퀀스에서 다른 항목이 생성되는 경우 해당 항목은 버퍼의 이전 항목을 대체하고 대기가 다시 시작됩니다. 다른 항목이 시퀀스에서 생성되기 전에 기한이 만료되는 경우 해당 항목은 시퀀스에 대한 구독을 통해 관찰됩니다.

예제

이 예제에서는 제한 연산자를 사용하여 항목이 2초 이하의 간격으로 관찰되도록 보장하는 방법을 보여 줍니다. 이는 EndlessBarrageOfEmails 메서드를 사용하여 관찰 가능한 시퀀스에서 항목으로 생성되는 전자 메일을 지속적으로 생성하는 방법을 보여 줍니다. 시퀀스의 전자 메일 항목은 서로 3초 이내에 임의 간격으로 발생합니다. 두 번째 시간 범위 동안 항목이 없는 상태에서 발생하는 항목만 시퀀스에서 관찰됩니다.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Reactive.Linq;
using System.Reactive.Concurrency;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*****************************************************************************************************//
      //*** Create an observable sequence from the enumerator which is yielding random emails within      ***//
      //*** 3 sec. continuously.  The enumeration of the enumerator will be scheduled to run on a thread  ***//
      //*** in the .NET thread pool so the main thread will not be blocked.                               ***//
      //*****************************************************************************************************//

      var obs = EndlessBarrageOfEmails().ToObservable(Scheduler.ThreadPool);


      //****************************************************************************************************//
      //*** Use the throttle operator to ONLY deliver an item that occurs with a 2 second interval       ***//
      //*** between it and the next item in the sequence. The throttle buffer will hold an item from the ***//
      //*** sequence waiting for the 2 second timespan to pass. If a new item is produced before the     ***//
      //*** time span expires, that new item will replace the old item in the buffer and the wait starts ***//
      //*** over. If the time span does expire before a new item is produced, then the item in the       ***//
      //*** buffer will be observed through any subscriptions on the sequence.                           ***//
      //***                                                                                              ***//
      //*** To be clear, an item is not guarnteed to be returned every 2 seconds. The use of throttle    ***//
      //*** here does guarntee that the subscriber will observe an item no faster than every 2 sec.      ***//
      //***                                                                                              ***//
      //*** Since a new email is generated at a random time within 3 seconds, the items which are        ***//
      //*** generated with 2 seconds of silence following them will also be random.                      ***//
      //***                                                                                              ***//
      //*** The timers associated with the 2 second time span are run on the .NET thread pool.           ***//
      //****************************************************************************************************//

      var obsThrottled = obs.Throttle(TimeSpan.FromSeconds(2), Scheduler.ThreadPool);


      //***********************************************************************************************//
      //*** Write each observed email to the console window. Also write a current timestamp to get  ***//
      //*** an idea of the time which has passed since the last item was observed. Notice, the time ***//
      //*** will not be less than 2 seconds but, will frequently exceed 2 sec.                      ***//
      //***********************************************************************************************//

      obsThrottled.Subscribe(i => Console.WriteLine("{0}\nTime Received {1}\n", i, DateTime.Now.ToString()));


      //*********************************************************************************************//
      //*** Main thread waiting on the user's ENTER key press.                                    ***//
      //*********************************************************************************************//

      Console.WriteLine("\nPress ENTER to exit...\n");
      Console.ReadLine();
    }




    //*********************************************************************************************//
    //***                                                                                       ***//
    //*** This method will continually yield a random email at a random interval within 3 sec.  ***//
    //***                                                                                       ***//
    //*********************************************************************************************//
    static IEnumerable<string> EndlessBarrageOfEmails()
    {
      Random random = new Random();

      //***************************************************************//
      //*** For this example we are using this fixed list of emails ***//
      //***************************************************************//
      List<string> emails = new List<string> { "Email Msg from John ", 
                                               "Email Msg from Bill ", 
                                               "Email Msg from Marcy ", 
                                               "Email Msg from Wes "};

      //***********************************************************************************//
      //*** Yield an email from the list continually at a random interval within 3 sec. ***//
      //***********************************************************************************//
      while (true)
      {
        yield return emails[random.Next(emails.Count)];
        Thread.Sleep(random.Next(3000));
      }
    }
  }
}

다음 출력은 예제 코드에서 생성되었습니다.

Press ENTER to exit...

Email Msg from Wes
Time Received 6/5/2011 11:54:05 PM

Email Msg from Marcy
Time Received 6/5/2011 11:54:08 PM

Email Msg from Bill
Time Received 6/5/2011 11:54:12 PM

Email Msg from Bill
Time Received 6/5/2011 11:54:15 PM

Email Msg from John
Time Received 6/5/2011 11:54:33 PM

Email Msg from Wes
Time Received 6/5/2011 11:54:35 PM

Email Msg from Marcy
Time Received 6/5/2011 11:54:38 PM

Email Msg from Bill
Time Received 6/5/2011 11:54:43 PM

참고 항목

참조

관찰 가능한 클래스

스로틀 오버로드

System.Reactive.Linq 네임스페이스