FromEventPattern如何知道事件何时完成



我有一个可观察的集合,它是由一堆事件填充的。我想从EventArg中获取信息,按名称分组,然后为每个名称选择最长日期。我试过这个:

_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")
                );

我想它可能会做我想做的事,但订阅永远不会完成:我从来没有得到完整的消息,也没有得到任何输出。我怀疑,这是因为Observable仍在等待更多的事件。我如何告诉它停止等待并给我输出?

如果您只等待来自服务的单个响应,请考虑在查询表达式中使用.Take(1)或.FirstAsync()

_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")
                );

相关内容

  • 没有找到相关文章

最新更新