ほんとは、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); } } } }