Reset and Dispose observable subscriber, Reactive Extensions

▼魔方 西西 提交于 2021-02-10 15:50:59

问题


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.


回答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




回答2:


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
    }
}


来源:https://stackoverflow.com/questions/45308073/reset-and-dispose-observable-subscriber-reactive-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!