Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing the following (simplified version of) class with System.Reactive. Right now, it ticks every 1 second, but in an effort to reduce workload on the CPU, I want to calculate the next datetime I'm interested in, and set a new interval accordingly. This new interval may or may not be different from the currently set interval. Here's the annotated class:

C#
public class Class1
{
    public IObservable<long> Timer;
    public IDisposable       TimerSub;
    DateTime                 NextTime = new DateTime(0);

    public Class1()
    {
        this.NextTime = new DateTime(0);
        this.Timer    = Observable.Timer(TimeSpan.FromSeconds(1));
        this.TimerSub = this.Timer.Subscribe(x => DoWork());
    }

    private void DoWork()
    {
        bool canWork = (this.NextTime.Ticks != 0);
        this.NextTime = this.CalculateFutureDate();

        // change the timer interval to  "this.NextTime - DateTime.Now"
        // how do I do this?

        if (canWork)
        {
            // Do some work stuff...
        }
    }

    private DateTime CalculateFutureDate()
    {
        return DateTime.Now.AddDays(1);
    }
}


What I have tried:

googling, but nothing came up that made any sense.
Posted
Updated 9-Aug-17 1:04am
Comments
Kornfeld Eliyahu Peter 8-Aug-17 9:48am    
With 'normal' timer you have to stop and re-start if you want to change interval...
I think that in this case you have to re-create the observable (and the subscription)...
(Also pay attention to the difference between Timer and Interval)
#realJSOP 8-Aug-17 9:54am    
You can't start/stop an Observable.Timer. There's no functionality for that
Kornfeld Eliyahu Peter 8-Aug-17 9:57am    
Of course - because it is not a 'real' timer... That's why I think you have to re-create (create a new) observable...
this.NextTime = this.CalculateFutureDate();
this.Timer = Observable.Timer(TimeSpan.FromSeconds([your-computation]));
this.TimerSub = this.Timer.Subscribe(x => DoWork());
#realJSOP 9-Aug-17 7:01am    
That was the easiest way to approach it and it seems to work fine, but it's not as convenient as having a method that sets the interval (or period) to a new value.
Kornfeld Eliyahu Peter 9-Aug-17 7:11am    
I think that in the case of observable it is not surprising...
By the idea you are creating an observable and left it alone, and maybe never see it again if the conditions are not fulfilled... And an observable can have only one (however complex) condition to fulfill... And in your case the observable actually out of any scope the moment the condition fulfilled (Timer - as you use it - is a one-timer)...
So I'm not surprised you can not reach out to the observable and change it...

1 solution

I ended up re-recreating the IObservable and re-subscribing as suggested above. I didn't like doing it that way, but it's the most expedient way to handle the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900