다음을 통해 공유


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

관찰 가능한 시퀀스의 각 요소를 타이밍 정보에 따라 생성되는 0개 이상의 버퍼로 나타냅니다.

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

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
    원본의 형식입니다.

매개 변수

  • source
    형식: System.IObservable<TSource>
    버퍼를 생성할 원본 시퀀스입니다.
  • timeShift
    형식: System.TimeSpan
    연속 버퍼를 만드는 간격입니다.

반환 값

형식: System.IObservable<IList<TSource>>
버퍼링된 관찰 가능한 시퀀스입니다.

사용 정보

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

설명

이 연산자는 timeSpan 매개 변수 기간 동안 발생하는 모든 항목을 보유하는 버퍼를 만듭니다. 이렇게 하면 애플리케이션이 항목을 일괄 처리로 배달할 수 있는 버퍼링할 수 있습니다. timeShift 매개 변수는 버퍼의 항목에 대해 구독 처리기를 실행해야 하는 빈도를 나타내며, 이로 인해 구독자에게 항목을 푸시합니다. 스케줄러 매개 변수는 버퍼에 대한 타이머를 만들 스레드를 제어합니다.

예제

예제 코드는 3초 이내에 임의로 전자 메일을 생성하는 IEnumerable에서 끝없는 전자 메일 시퀀스를 생성합니다. 전자 메일은 IObservable.TimeStamp 연산자를 사용하여 타임스탬프가 적용됩니다. 그런 다음 10초 범위 내에 발생하는 모든 전자 메일을 보유하는 버퍼로 버퍼링됩니다. 버퍼링된 시퀀스에 대한 구독이 만들어집니다. 마지막으로 각 전자 메일 그룹은 전자 메일에 대해 생성된 해당 타임스탬프와 함께 콘솔 창에 기록됩니다.

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

참고 항목

참조

관찰 가능한 클래스

버퍼 오버로드

System.Reactive.Linq 네임스페이스