Observable.Throttle<TSource> メソッド (IObservable<TSource>、TimeSpan、IScheduler)

指定したソース、dueTime、およびスケジューラを使用して、期限前に別の値が続く監視可能なシーケンスの値を無視します。

Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'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
    ソースの種類。

パラメーター

  • source
    種類: System.IObservable<TSource>
    スロットルするソース シーケンス。
  • Duetime
    種類: System.TimeSpan
    各値のスロットル期間の期間。

戻り値

種類: System.IObservable<TSource>
観察可能なシーケンスの値。その後に期限前に別の値が続きます。

使用上の注意

Visual Basic と C# では、 IObservable<TSource> 型の任意のオブジェクトのインスタンス メソッドとしてこのメソッドを呼び出すことができます。 インスタンス メソッド構文を使用してこのメソッドを呼び出す場合は、最初のパラメーターを省略します。 詳細については、」または」を参照してください。

解説

Throttle 演算子は、シーケンスの項目をバッファーに格納し、dueTime パラメーターで指定された期間が期限切れになるまで待ちます。 期間が切れる前にシーケンスから別の項目が生成された場合、その項目はバッファー内の古い項目を置き換え、待機はやり直します。 シーケンス内で別の項目が生成される前に期限切れになった場合、そのアイテムはシーケンスへの任意のサブスクリプションを通じて観察されます。

この例では、スロットル演算子を使用して、項目が 2 秒未満の間隔で観察されることを保証する方法を示します。 これを示すには、EndlessBarrageOfEmails メソッドを使用して、監視可能なシーケンス内のアイテムとして生成される電子メールを継続的に生成します。 シーケンス内のメール アイテムは、互いに 3 秒以内にランダムに行われます。 2 番目の期間に続く項目がない状態で発生した項目のみが、シーケンスから観察されます。

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

参照

リファレンス

Observable クラス

スロットル オーバーロード

System.Reactive.Linq 名前空間