重置和处置可观察订阅者

重置和处置可观察订阅者

本文介绍了重置和处置可观察订阅者、反应性扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个:

    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-在CheckUploadSetList中我想处理或完成observable

1- in CheckUploadSetList I want to dispose or finish observable

2-在AddDicomFile我想重置它

作为方法中的注释.

更新:

我可以通过 Timer 来实现:

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

提前致谢.

推荐答案

将 Reactive Extension 仅用于某些计时器功能对我来说似乎有点矫枉过正.为什么不为此使用普通计时器,并在给定时间启动/停止它?

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?

让我给个主意.

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

背景材料

我真的可以推荐这些网站:

I really can recommend these sites:

http://www.introtorx.com/

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

这篇关于重置和处置可观察订阅者、反应性扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:41