Observable::Interval Method (TimeSpan, IScheduler)

Returns an observable sequence that produces a value after each period.

Namespace:  System.Reactive.Linq
Assembly:  System.Reactive (in System.Reactive.dll)

static IObservable<long long>^ Interval(
	TimeSpan period, 
	IScheduler^ scheduler


Type: System::TimeSpan
The period for producing the values in the resulting sequence.
Type: System.Reactive.Concurrency::IScheduler
The scheduler to run the timer on.

Return Value

Type: System::IObservable<Int64>
An observable sequence that produces a value after each period.

The Interval operator generates a sequence of long integers. Each integer is generated after the timespan provided for the period parameter expires. The scheduler parameter can be used to schedule the generation of integers using any of the provided scheduler or a custom scheduler. For example the NewThreadScheduler could be used to execute the generation of the integers on a new thread.

The following example code uses the Interval operator to generate a sequence of long integers staring at zero. Each integer in the sequence is generated after the two second period has expired. Each integer is written to the console window along with the current time to show the period parameter in effect. The generation of the integer sequence is scheduled to run on a thread from the .Net thread pool so the main thread is not blocked and can process the enter key being pressed to stop the interval.

using System;
using System.Reactive.Linq;
using System.Reactive.Concurrency;

namespace Example
  class Program
    static void Main()
      //*** Generate a sequence of integers starting at zero until ENTER is pressed.              ***//
      //***                                                                                       ***//
      //*** A new integer will be generated by the Interval operator after each 2 second period   ***//
      //*** expires.                                                                              ***//
      //***                                                                                       ***//
      //*** By using the ThreadPool scheduler, the sequence of integers will be generated by a    ***//
      //*** thread in the .NET thread so the main thread is not blocked.                          ***//

      const int periodInSec = 2;
      var obs = Observable.Interval(TimeSpan.FromSeconds(periodInSec),

      //*** Write each value from Interval to the console window along with the current time to   ***//
      //*** show the period time span in effect.                                                  ***//

      using (IDisposable handle = obs.Subscribe(x => Console.WriteLine("Integer : {0}\tCurrent Time : {1}", x, DateTime.Now.ToLongTimeString())))
        Console.WriteLine("Press ENTER to exit...\n");

The following output was generated by the example code.

Press ENTER to exit...

Integer : 0     Current Time : 5:11:55 PM
Integer : 1     Current Time : 5:11:57 PM
Integer : 2     Current Time : 5:11:59 PM
Integer : 3     Current Time : 5:12:01 PM
Integer : 4     Current Time : 5:12:03 PM
Integer : 5     Current Time : 5:12:05 PM
Integer : 6     Current Time : 5:12:07 PM
Integer : 7     Current Time : 5:12:09 PM
Integer : 8     Current Time : 5:12:11 PM
Integer : 9     Current Time : 5:12:13 PM
Integer : 10    Current Time : 5:12:15 PM
Integer : 11    Current Time : 5:12:17 PM
Integer : 12    Current Time : 5:12:19 PM
Integer : 13    Current Time : 5:12:21 PM