Run a task immediately over a time interval using the Rx frame

I try to run my task immediately and then run it in a time interval. I wrote the following:

var syncMailObservable = Observable.Interval(TimeSpan.FromSeconds(15)); syncMailObservable.Subscribe(s => MyTask()); 

The problem is that the task starts only after 15 seconds. I need to run my task at the beginning, and then continue at a time interval.

How can I do it?

+8
system.reactive
source share
3 answers

You can do it:

 var syncMailObservable = Observable .Interval(TimeSpan.FromSeconds(15.0), Scheduler.TaskPool) .StartWith(-1L); syncMailObservable.Subscribe(s => MyTask()); 
+14
source share

Try the following:

 Observable.Return(0).Concat(Observable.Interval(TimeSpan.FromSeconds(15))) .Subscribe(_ => MyTask()); 
+3
source share

This question is old and applies specifically to the Interval method, but the Timer method can be used to achieve this.

The Timer method supports the initial delay (runtime). Setting it as a zero time interval should start the task immediately, and then run it at each interval.

  var initialDelay = new TimeSpan(0); var interval = TimeSpan.FromSeconds(15); Observable.Timer(initialDelay, interval, Scheduler.TaskPool) .Subscribe(_ => MyTask()); 

https://msdn.microsoft.com/en-us/library/hh229652(v=vs.103).aspx

+2
source share

All Articles