[英]Reset and Dispose observable subscriber, Reactive Extensions
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 1-在
CheckUploadSetList
我要配置或完成可观察
2- in AddDicomFile
I want to reset it 2-在
AddDicomFile
我要重置它
as comment in methods. 作为方法注释。
UPDATE: 更新:
I can do it by Timer
as: 我可以通过
Timer
来做到这一点:
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. 提前致谢。
Using Reactive Extension for just some timer function seems a bit overkill to me. 对我来说,仅将Reactive Extension用于某些计时器功能似乎有些矫kill过正。 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://www.introtorx.com/
http://rxwiki.wikidot.com/101samples http://rxwiki.wikidot.com/101samples
You should use Switch()
for this kind of thing. 您应该将
Switch()
用于此类事情。
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.