I have a webservice that I want to query for updates immediately after a boolean is set to true, and then recheck every 5 minutes.
Here is my current observable:
_queryDisposable = Observable
.Interval(TimeSpan.FromMinutes(5))
.ObserveOn(Scheduler.ThreadPool)
.Where(i => IsProcessing) // IsProcessing is the bool value
.Subscribe(GetFeeds, OnError, OnComplete);
This observable will check if the IsProcessing bool is true or false every 5 mins and then call GetFeeds if it is true.
The only way that I can think to get the desired effect is to make IsProcessing a property backed field and process with 2 observables like the following:
private bool _isProcessing;
public bool IsProcessing
{
get { return _isProcessing; }
set
{
if (_isProcessing == value)
return;
_isProcessing = value;
if (!value)
{
if(_queryDisposable != null)
_queryDisposable.Dispose();
_queryDisposable = null;
}
else
{
Observable
.Range(0,1)
.ObserveOn(Scheduler.ThreadPool)
.Subscribe(GetFeedsSafe, OnError, OnComplete);
_queryDisposable = Observable
.Interval(TimeSpan.FromMinutes(5))
.ObserveOn(Scheduler.ThreadPool)
.Subscribe(GetFeedsSafe, OnError, OnComplete);
}
}
}
I don’t think that is a very elegant solution, and so I’d like to know if there is a better way to acheive this effect?
Am I missing something, or wouldn’t you just call Subscribe yourself immediately before starting the Observable?
EDIT: Pure Rx, from http://social.msdn.microsoft.com/Forums/sa/rx/thread/c4acaf34-3136-4206-a6f9-ef5afba74b2b: