Reputation: 472
I am using one delegate and event for the same as below:
public delegate void DelgSampledelegate(string status);
public static event DelgSampledelegate sampleEvent;
sampleEvent += new DelgSampledelegate(sample_method);
public void sample_method(string value)
{}
Now I wanted to use Rx Extension for above delegate. So I tried below code to create object of Observable.FromEvent
var objDataUpdated = Observable.FromEvent(h => sampleEvent += h, h => sampleEvent -= h);
var movesSubscription = objDataUpdated.Subscribe(evt => evt.ToString());
My aim is to call 'sample_method'
function as I was calling earlier.I know it may be done either via subscribe.Please guide me proper way to do it.
Upvotes: 1
Views: 168
Reputation: 29796
Can't see that you're missing much except the type parameters and actually raising an event to test it:
var objDataUpdated = Observable.FromEvent<DelgSampledelegate, string>(
h => sampleEvent += h,
h => sampleEvent -= h);
var movesSubscription = objDataUpdated.Subscribe(x => sample_method(x));
// note the preceding line can be shortened to finish
// .Subscribe(sample_method) but I didn't want to make too many leaps
// raise a sampleEvent, will call sample_method("Test")
sampleEvent("Test");
See How to use Observable.FromEvent instead of FromEventPattern and avoid string literal event names for a comprehensive explanation of FromEvent
- although note the form in this answer needs no conversion function because DelgSampledelegate
's signature matches the required OnNext
signature.
Upvotes: 1