对不起,不是故意挑你,但:
ALWAYS处置IDISPOSABLES !!!!!
(编辑:好吧,不知道我今天早上把我的咖啡,但我回答废话的整个混乱的挫折感,我将离开上述只是因为一般,你想确保处置任何IDisposable
,但在努力弥补下面的潺潺......)
调用到
Subscribe
创建一个订阅,你是不是配置,所以多次调用该方法只是要排队越来越多的废话 - 现在在这个特定的情况下,它不是世界末日,因为
Timer
只发射一次,但仍然...处置!
如果你真的想使用这种方法(我认为更好的方法是让一些正在运行的线程/任务“倾向于”你的值,当它认为它的必要时删除),至少尝试类似于:
好的,忽略所有这些剔除的废话。的Observable.Timer
实现是这样的:
public static IObservable<long> Timer(TimeSpan dueTime)
{
return s_impl.Timer(dueTime);
}
进而调用这个:
这就要求...
private static IObservable<long> Timer_(TimeSpan dueTime, IScheduler scheduler)
{
return new Timer(dueTime, null, scheduler);
}
这里的事情变得有趣 - Timer
是Producer<long>
,其中肉味的位是:
private IDisposable InvokeStart(IScheduler self, object state)
{
this._pendingTickCount = 1;
SingleAssignmentDisposable disposable = new SingleAssignmentDisposable();
this._periodic = disposable;
disposable.Disposable = self.SchedulePeriodic<long>(1L, this._period, new Func<long, long>(this.Tock));
try
{
base._observer.OnNext(0L);
}
catch (Exception exception)
{
disposable.Dispose();
exception.Throw();
}
if (Interlocked.Decrement(ref this._pendingTickCount) > 0)
{
SingleAssignmentDisposable disposable2 = new SingleAssignmentDisposable {
Disposable = self.Schedule<long>(1L, new Action<long, Action<long>>(this.CatchUp))
};
return new CompositeDisposable(2) { disposable, disposable2 };
}
return disposable;
}
现在,base._observer.OnNext
,这是内部的水槽设置为触发的计时器滴答,其中在该Invoke
是:
private void Invoke()
{
base._observer.OnNext(0L);
base._observer.OnCompleted();
base.Dispose();
}
所以,是的。它自动处理自己 - 并且不会有任何“闲置的订阅”浮动。
嗯....乌鸦很好吃。:|
AddItem(”foo“); 30秒后的AddItem(”foo“);? – Phil 2013-03-24 14:23:28