c# - Reactive extensions dispose event handler -
hi guys have externaldataservice firing real time data, contained in lib.
i have wrapper class subscribes , puts updates on observable..
public class myservice { private externaldataservice _externaldataservice; public myservice() { _externaldataservice= new externaldataservice (); } public iobservable<double> getdata() { return observable.create<double>(i => { _externalpriceservice.ondatachanged += (s, e) => { i.onnext(e); }; return () => { // do here? }; }); } }
consumed as...
var p = new myservice(); var disposable = p.getdata().subscribe(i => console.writeline(i));
how unsubscribe _externalpriceservice.ondatachanged
when dispose
called on disposable?
use observable.fromevent or observable.fromeventpattern, instead of observable.create. dispose subscription.
depending on eventhandler definition, this:
public class externaldataservice { public eventhandler<datachangedeventargs> ondatachanged; } public class datachangedeventargs : eventargs { public double data { get; set; } } public class myservice { private externaldataservice _externaldataservice; public myservice() { _externaldataservice= new externaldataservice (); } public iobservable<double> getdata() { return observable.fromeventpattern<datachangedeventargs>(eh => _externaldataservice.ondatachanged += eh, eh => _externaldataservice.ondatachanged -= eh) .select(e => e.eventargs.data); } }
Comments
Post a Comment