I'm trying to run my task immediately, then to run it by time interval. I wrote the following :
var syncMailObservable = Observable.Interval(TimeSpan.FromSeconds(15));
syncMailObservable.Subscribe(s => MyTask());
The problem is the task starts only after the 15 seconds. I need to run my task at the beginning then to continue by time interval.
How would I do that?
You could do this:
var syncMailObservable =
Observable
.Interval(TimeSpan.FromSeconds(15.0), Scheduler.TaskPool)
.StartWith(-1L);
syncMailObservable.Subscribe(s => MyTask());
Try this:
Observable.Return(0).Concat(Observable.Interval(TimeSpan.FromSeconds(15)))
.Subscribe(_ => MyTask());
This question refers to the Interval method specifically, but the Timer method can be used to accomplish this cleanly.
The Timer method supports an initial delay (due time). Setting it as a time span of zero should start the task at once, 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With