简体   繁体   中英

How does FromEventPattern know when events are completed?

I have an observable collection that is filled from a load of events. I want to take information from the EventArg, group it by name and then select the maximum date for each name. I have tried this :

_subscription = Observable
            .FromEventPattern<NewLoanEventHandler, NewLoanEventArgs>(
                h => loan.NewLoanEvent += h, 
                h => loan.NewLoanEvent -= h)
            .Select(a => a.EventArgs.Counterpatry)
            .GroupBy(c => c.Name)
            .SelectMany(grp => grp.Max( c => c.MaturityDate ).Select( maturity => new {grp.Key, maturity}) )
            .Subscribe( 
                i => Console.WriteLine("{0} --> {1}", i.Key, i.maturity),
                Console.WriteLine,
                () => Console.WriteLine("completed")
                );

I think it might do what I want, but the subscription never completes : I never get the complete message and I don't get any output. That is, I suspect, because the Observable is still waiting for more events. How do I tell it to stop waiting and give me my output?

If you are only waiting for a single response from your service, consider using .Take(1) or .FirstAsync() in the query expression:

_subscription = Observable
            .FromEventPattern<NewLoanEventHandler, NewLoanEventArgs>(
                h => loan.NewLoanEvent += h, 
                h => loan.NewLoanEvent -= h)
            .Take(1)
            .Select(a => a.EventArgs.Counterpatry)
            .GroupBy(c => c.Name)
            .SelectMany(grp => grp.Max( c => c.MaturityDate ).Select( maturity => new {grp.Key, maturity}) )
            .Subscribe( 
                i => Console.WriteLine("{0} --> {1}", i.Key, i.maturity),
                Console.WriteLine,
                () => Console.WriteLine("completed")
                );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM