Rx/IO接続関連

Last-modified: 2020-02-27 (木) 21:05:12

メモ

public void ConcatTest()
{
    var sw = new Stopwatch();
    var cts = new CancellationTokenSource();

    Action<string> Output = tag => Console.WriteLine($"{sw.ElapsedMilliseconds}\t:{tag}({Thread.CurrentThread.ManagedThreadId})");

    sw.Start();
    Output("Start");

    Observable.Timer(TimeSpan.FromSeconds(3))
        .Do(_ => Output("Do_A"))
        .Concat(Observable.Timer(TimeSpan.FromSeconds(1)).Do(_ => Output("Do_B")))
        .Do(_ => Output("Do_C"))
        .Subscribe(_ => Output("OnNext"), () => cts.Cancel());

    Output("WaitStart");
    cts.Token.WaitHandle.WaitOne(10000);
    Output("WaitEnd");
}

出力

0	:Start(15)
43	:WaitStart(15)
3046	:Do_A(7)
3046	:Do_C(7)
3047	:OnNext(7)
4054	:Do_B(10)
4054	:Do_C(10)
4054	:OnNext(10)
4056	:WaitEnd(15)

Do_Cの部分でたまにやらかす(´・ω・`)