1

Suppose I have this :

    public class UploadDicomSet 
{ 
    public UploadDicomSet()
    {
        var cachCleanTimer = Observable.Interval(TimeSpan.FromMinutes(2));
        cachCleanTimer.Subscribe(CheckUploadSetList);
        //Start subscriber
    }
    void CheckUploadSetList(long interval)
    {
        //Stop and dispose subscriber
    }
    public void AddDicomFile(SharedLib.DicomFile dicomFile)
    {
        //Renew subscriber, call CheckUploadSetList 2 minutes later
    }
}

1- in CheckUploadSetList I want to dispose or finish observable

2- in AddDicomFile I want to reset it

as comment in methods.

UPDATE:

I can do it by Timer as:

 public class UploadDicomSet : ImportBaseSet
{
    Timer _timer;
    public UploadDicomSet()
    {
        _timer = new Timer(CheckUploadSetList, null, 120000, Timeout.Infinite);
    }

    void CheckUploadSetList(object state)
    {
        Logging logging = new Logging(LogFile);
        try
        {
            _timer.Dispose(); //Stop the subscription
                              //dispose everything
        }
        catch (Exception exp)
        {
            logging.Log(ErrorCode.Error, "CheckUploadSetList() failed..., EXP:{0}", exp.ToString());
        }
    }
    public void AddDicomFile(SharedLib.DicomFile dicomFile)
    {
        _timer.Change(120000, Timeout.Infinite);
    }
}

Thanks in advance.

Aria
  • 3,724
  • 1
  • 20
  • 51

2 Answers2

3

You should use Switch() for this kind of thing.

Something like this:

public class UploadDicomSet : ImportBaseSet
{
    IDisposable subscription;
    Subject<IObservable<long>> subject = new Subject<IObservable<long>>();

    public UploadDicomSet()
    {
        subscription = subject.Switch().Subscribe(s => CheckUploadSetList(s));
        subject.OnNext(Observable.Interval(TimeSpan.FromMinutes(2)));
    }

    void CheckUploadSetList(long interval)
    {
        subject.OnNext(Observable.Never<long>());
        // Do other things
    }

    public void AddDicomFile(SharedLib.DicomFile dicomFile)
    {
        subject.OnNext(Observable.Interval(TimeSpan.FromMinutes(2)));
        // Reset the subscription to go off in 2 minutes from now
        // Do other things
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Thanks for answer, what `switch` really do ? – Aria Jul 31 '17 at 06:02
  • @Aria - `.Switch()` switches to the latest inner observable. So it flattens an `IObservable>` to `IObservable` by only outputting values from the latest inner observable. It means you don't have to unsubscribe and re-subscribe to get entirely new values from an observable. – Enigmativity Jul 31 '17 at 07:48
  • Up+, So you think it is more efficient than the old – Aria Jul 31 '17 at 07:58
  • @Aria - It is more robust. If you use this to pass the observable (`subject.Switch()`) out of the class then you don't know who subscribes to it, but it will still produce values. The other class you must know who has subscribed to re-subscribe them. Does that make sense? – Enigmativity Jul 31 '17 at 08:06
1

Using Reactive Extension for just some timer function seems a bit overkill to me. Why not just use an ordinary timer for this, and start/stop it at given times?

Let me give an idea.

public class UploadDicomSet : ImportBaseSet
{
    IDisposable subscription;

    public void CreateSubscription()
    {
        var cachCleanTimer = Observable.Interval(TimeSpan.FromMinutes(2));

        if(subscription != null)
            subscription.Dispose();

        subscription = cachCleanTimer.Subscribe(s => CheckUploadSetList(s));
    }

    public UploadDicomSet()
    {
        CreateSubscription();
        // Do other things
    }

    void CheckUploadSetList(long interval)
    {
        subscription.Dispose(); // Stop the subscription
        // Do other things
    }

    public void AddDicomFile(SharedLib.DicomFile dicomFile)
    {
        CreateSubscription(); // Reset the subscription to go off in 2 minutes from now
        // Do other things
    }
}

Background material

I really can recommend these sites:

http://www.introtorx.com/

http://rxwiki.wikidot.com/101samples

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • It doesn't work, with Option2 , the `CheckUploadSetList` is calling every 2 minutes even after `subscription.Dispose()`. – Aria Jul 26 '17 at 05:11
  • Thanks for your attention, I didn't test option 1, I was debugging it and set break-point there it is calling infinity, about option 1 I will test it and inform you about result. – Aria Jul 26 '17 at 05:59
  • Let me post my code which is tested, I want call `CheckUploadSetList` if there is no change for two minutes, in `AddDicomFile` a new change arrived so subscriber should be renew for two minutes later, if `CheckUploadSetList` called for one time I want dispose everything there, I did Option 2 it doesn't work , I will test both of them and tell you feedback. – Aria Jul 26 '17 at 06:17
  • I've update my answer, it mimics the timer functionality – Peter Bons Jul 26 '17 at 06:34
  • Yes that is, it works like a charm with your new update. – Aria Jul 26 '17 at 06:59