Thursday, 20 June 2013

Delay


Delay method is used to delay the stream for a certain period of time. Please take care that this will be storing the elements lapsed during the delayed time interval in memory, so this needs to be used very carefully as the memory can grow quite quickly.

Let us take an example:-

        static void Main(string[] args)
        {
            var myObservable = Observable.Interval(TimeSpan.FromSeconds(1));
         
            myObservable.Delay(TimeSpan.FromSeconds(5)).Subscribe(num =>
            {
                Console.WriteLine(num);
            });        
         
           Console.WriteLine("Press any key to unsubscribe");
           Console.ReadKey();
                     
        }


This will create a stream which is delayed by 5 seconds. Note that the elements observed in every five second window will be cached and replayed.




No comments:

Post a Comment