Beispiel #1
0
        /// <summary>
        /// Task 生成 Observable,使用 ToObservable。
        /// 这个写法很简单,而且执行时间是耗时最长的 Task 时间,跟 AsyncAwaitGenerate 不一样
        /// </summary>
        /// <param name="amout"></param>
        /// <returns></returns>
        public static IObservable <int> TaskToObservable(int amout)
        {
            var o1 = PrimeGenerator.GenerateAsync(5).ToObservable();
            var o2 = PrimeGenerator.GenerateAsync(5).ToObservable();

            return(o1.Concat(o2)                   // 先用 Concat 连接两个集合
                   .SelectMany(primes => primes)); // 再用 SelectMany 投影每个值
        }
Beispiel #2
0
        /// <summary>
        /// AsyncAwait 模式生成Observable
        /// 执行时间等于所有task总和
        /// </summary>
        /// <param name="amout"></param>
        /// <returns></returns>
        public static IObservable <int> AsyncAwaitGenerate(int amout)
        {
            return(Observable.Create <int>(async o =>
            {
                var primes1 = await PrimeGenerator.GenerateAsync(5);
                foreach (var prime in primes1)
                {
                    o.OnNext(prime);
                }

                var primes2 = await PrimeGenerator.GenerateAsync(5);
                foreach (var prime in primes2)
                {
                    o.OnNext(prime);
                }
                o.OnCompleted();
            }));
        }
Beispiel #3
0
 /// <summary>
 /// 周期性地生成 Observable,均匀时间间隔
 /// </summary>
 /// <returns></returns>
 public static IObservable <int> CreateByInterval()
 {
     return(Observable.Interval(TimeSpan.FromSeconds(2))
            .SelectMany(_ => PrimeGenerator.GenerateAsync(3))
            .SelectMany(primes => primes));
 }