共用方式為


Observable.Buffer < TSource > 方法 (IObservable < TSource > , TimeSpan, TimeSpan, IScheduler)

會根據計時資訊,將可觀察序列的每個元素指出為零個或多個緩衝區。

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

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function Buffer(Of TSource) ( _
    source As IObservable(Of TSource), _
    timeSpan As TimeSpan, _
    timeShift As TimeSpan, _
    scheduler As IScheduler _
) As IObservable(Of IList(Of TSource))
'Usage
Dim source As IObservable(Of TSource)
Dim timeSpan As TimeSpan
Dim timeShift As TimeSpan
Dim scheduler As IScheduler
Dim returnValue As IObservable(Of IList(Of TSource))

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

類型參數

  • TSource
    來源的類型。

參數

  • timeShift
    類型: System.TimeSpan
    建立連續緩衝區之間的間隔。

傳回值

類型:System.IObservable<IList< TSource>>
緩衝的可觀察序列。

使用注意事項

在 Visual Basic 和 C# 中,您可以在IObservable< TSource > 類型的任何物件上呼叫此方法作為實例方法。 使用執行個體方法語法呼叫這個方法時,請省略第一個參數。 如需詳細資訊,請參閱

備註

此運算子會建立緩衝區,以保存 timeSpan 參數期間發生的所有專案。 這可讓應用程式以批次方式傳遞專案。 timeShift 參數會指出緩衝區中的專案應該執行訂閱處理常式的頻率,這會導致將專案推送至訂閱者。 排程器參數會控制要建立緩衝區計時器的執行緒。

範例

此範例程式碼會從 IEnumerable 產生無限的電子郵件序列,以隨機在三秒內產生電子郵件。 電子郵件是使用 IObservable.TimeStamp 運算子來時間戳記。 然後,它們會緩衝到緩衝區,以保存在十秒內發生的所有電子郵件。 系統會建立緩衝序列的訂用帳戶。 最後,每個電子郵件群組接著都會寫入主控台視窗,以及針對電子郵件產生的對應時間戳記。

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

namespace Example
{

  class Program
  {
    static void Main()
    {
      //************************************************************************************************************************//
      //*** By generating an observable sequence from the enumerator, we can use Rx to push the emails to an email buffer    ***//
      //*** and have the buffer dumped at an interval we choose. This simulates how often email is checked for new messages. ***//
      //************************************************************************************************************************//
      IObservable<string> myInbox = EndlessBarrageOfEmails().ToObservable();


      //************************************************************************************************************************//
      //*** We can use the Timestamp operator to additionally timestamp each email in the sequence when it is received.      ***//
      //************************************************************************************************************************//
      IObservable<Timestamped<string>> myInboxTimestamped = myInbox.Timestamp();


      //******************************************************************************************************************************//
      //*** The timer controls the frequency of emails delivered from the email buffer. This timer will be on another thread since ***//
      //*** the main thread will be blocked waiting on a key press.                                                                ***//
      //******************************************************************************************************************************//
      System.Reactive.Concurrency.IScheduler scheduleOnNewThread = System.Reactive.Concurrency.Scheduler.NewThread;


      //***************************************************************************************************************************//
      //*** Create a buffer with Rx that will hold all emails received within 10 secs and execute subscription handlers for the ***//
      //*** buffer every 10 secs.                                                                                               ***//
      //*** Schedule the timers associated with emptying the buffer to be created on the new thread.                            ***//
      //***************************************************************************************************************************//
      IObservable<IList<Timestamped<string>>> newMail = myInboxTimestamped.Buffer(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), 
                                                                                  scheduleOnNewThread);


      //******************************************************//
      //*** Activate the subscription on a separate thread ***//
      //******************************************************//
      IDisposable handle = newMail.SubscribeOn(scheduleOnNewThread).Subscribe(emailList =>
      {
        Console.WriteLine("\nYou've got mail!  {0} messages.\n", emailList.Count);
        foreach (Timestamped<string> email in emailList)
        {
          Console.WriteLine("Message   : {0}\nTimestamp : {1}\n", email.Value, email.Timestamp.ToString());
        }
      });

      Console.ReadLine();
      handle.Dispose();
    }



    //*********************************************************************************************//
    //***                                                                                       ***//
    //*** 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));
      }
    }
  }
}

以下是範例程式碼的範例輸出。

You've got mail!  6 messages.

Message   : Email Msg from John
Timestamp : 5/16/2011 3:45:09 PM -04:00

Message   : Email Msg from Wes
Timestamp : 5/16/2011 3:45:12 PM -04:00

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:13 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:13 PM -04:00

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:13 PM -04:00

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:15 PM -04:00


You've got mail!  7 messages.

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:17 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:18 PM -04:00

Message   : Email Msg from Wes
Timestamp : 5/16/2011 3:45:19 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:21 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:24 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:26 PM -04:00

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:26 PM -04:00

另請參閱

參考

Observable 類別

緩衝區多載

System.Reactive.Linq 命名空間