Observable.Interval Wait for the action to complete.

Observable.Interval produces a value every time period. How to make it wait until the action ends before the next iteration?

Example:

Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(async t => 
{
    //Here could be some long running action with different duration each iteration
    Console.WriteLine(t.ToString());
    await Task.Delay(3000);
});

He will begin the action every second. How to make him wait for the action to complete?

+4
source share
2 answers

What you're asking for is the default behavior - unless you imagine async/ await- so remove asyncand use Task.Delay(3000).Wait().

Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(t => 
{
    //Here could be some long running action with different duration each iteration
    Console.WriteLine(t.ToString());
    Task.Delay(3000).Wait();
});

, - async.

Observable.Interval(TimeSpan.FromSeconds(1)) - () .

, , .

+2

? TaskFactory.StartNew while recursive. Task.Delay

-1

Source: https://habr.com/ru/post/1657488/


All Articles