如何在我的代码中处置 IDisposable
本文关键字:IDisposable 代码 我的 | 更新日期: 2023-09-27 18:32:21
在我一直在研究的以下类的CreateRegistryObservable
方法中,我无法弄清楚如何干净地处理下一行分配的IDisposable
。这样做的问题是,我从分配给onComplete
委托的Action
中引用它,该委托使用 {null} 初始化捕获声明的数组变量,并且在调用此操作时不会反映通过调用 Schedule()
在最后返回的内容的赋值。如何干净地处置调度程序?
scheduler[0] = Scheduler.CurrentThread.Schedule(iterator);
public class RegistryKeyMonitor<T> : IObservable<T>
{
private readonly RegistryKey _registryKey;
private readonly string _name;
private readonly TimeSpan _timeSpan;
private readonly ILog _logger = LogManager.GetLogger("RegistryKeyMonitor");
public RegistryKeyMonitor(RegistryKey registryKey, string name, TimeSpan timeSpan)
{
_registryKey = registryKey;
_name = name;
_timeSpan = timeSpan;
}
public IDisposable Subscribe(IObserver<T> observer)
{
var sub =CreateRegistryObservable(_registryKey, _name, _timeSpan)
//.Catch(Observable.Empty<object>())
.Where(obj => obj != null)
.Cast<T>();
return sub.Subscribe(observer);
}
private IObservable<object> CreateValueChangeObservable(RegistryKey registryKey, string name, object initialValue,
TimeSpan timeSpan, CancellationToken token)
{
return Observable.Create<object>(o =>
{
var scheduler = Scheduler.Immediate;
Action<object, Action<object, TimeSpan>> iterator = (current, self) =>
{
try
{
if (token.IsCancellationRequested)
{
o.OnCompleted();
return;
}
var value = registryKey.GetValue(name);
_logger.DebugFormat("{0} == registryKey.GetValue(name)", value);
if (current == null && value != null ||
current != null && !current.Equals(value))
{
_logger.DebugFormat("passing data {0}", value);
o.OnNext(value);
}
else
self(value, timeSpan);
}
catch (Exception e)
{
o.OnError(e);
}
};
return scheduler.Schedule(initialValue, timeSpan, iterator);
});
}
public IObservable<object> CreateRegistryObservable(RegistryKey registryKey, string name, TimeSpan timeSpan)
{
return Observable.Create<object>(o =>
{
var cancel = new CancellationDisposable();
return new CompositeDisposable(cancel,
NewThreadScheduler.Default.Schedule(() =>
{
IDisposable[] scheduler = {null};
var currentStateSubscription = new SerialDisposable();
object currentValue = null;
Action<Action> iterator = self =>
currentStateSubscription.Disposable =
CreateValueChangeObservable(registryKey, name, currentValue,
timeSpan, cancel.Token)
.Subscribe(value =>
{
currentValue = value;
self();
o.OnNext(value);
},
o.OnError,
()=> {
currentStateSubscription.Dispose();
scheduler[0].Dispose();
}
);
scheduler[0] = Scheduler.CurrentThread.Schedule(iterator);
}));
});
}
}
我自己想通了,如下。
public IObservable<object> CreateRegistryObservable(RegistryKey registryKey, string name, TimeSpan timeSpan)
{
return Observable.Create<object>(o =>
{
var cancel = new CancellationDisposable();
var currentStateSubscription = new SerialDisposable();
object currentValue = null;
return new CompositeDisposable(cancel, NewThreadScheduler.Default.Schedule(
self => currentStateSubscription.Disposable =
CreateValueChangeObservable(registryKey, name, currentValue,
timeSpan, cancel.Token)
.Subscribe(value =>
{
currentValue = value;
self();
o.OnNext(value);
},
o.OnError,
currentStateSubscription.Dispose
)
));
});
}