Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

接続中のみ処理したい…!

$
0
0

ほんとは、whileループの中はHttpClient使った処理あたり・・・?う~ん、無理やり感があるかなぁ?というメモ。

using System;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        staticvoid Main(string[] args)
        {
            var connectionSubject = new Subject<bool>();
            var connectObservable = connectionSubject.Where(b => b).AsObservable();
            var disconnectObservable = connectionSubject.Where(b => !b).AsObservable();
            var src = connectObservable.SelectMany(_ =>
                {
                    return Observable.Create<string>(async o =>
                    {
                        Console.WriteLine("connection start!");
                        var connected = true;
                        while (connected)
                        {
                            o.OnNext("foo");
                            await Task.Delay(1000);
                            o.OnNext("ooo");
                            await Task.Delay(1000);
                        }
                    });
                })
                .TakeUntil(disconnectObservable)
                .Repeat();

            src.ObserveOn(Scheduler.Default).Subscribe(Console.WriteLine);
            while (true)
            {
                Console.ReadKey();
                Console.WriteLine("OnNext(true)");
                connectionSubject.OnNext(true);
                Console.ReadKey();
                Console.WriteLine("OnNext(false)");
                connectionSubject.OnNext(false);
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>