mark
mark

Reputation: 62764

Why the Rx.NET Finally block is not called?

Please, observe the following simple Rx.NET program:

using System;
using System.Diagnostics;
using System.Reactive.Linq;

namespace observables
{
    class Program
    {
        static void Main()
        {
            var src = Observable
                .Interval(TimeSpan.FromMilliseconds(1))
                .Take(1)
                .Do(o => Debug.WriteLine(o))
                .Finally(() => Debug.WriteLine("************ Finally"));
            src.GetAwaiter().GetResult();
        }
    }
}

It displays:

0
************ Finally

Now, when I remove the Do block from the monad, the program does not display anything!

Why?

Upvotes: 1

Views: 465

Answers (1)

supertopi
supertopi

Reputation: 3488

There is a race condition between the Observable thread and the Main thread.

The function call src.GetAwaiter().GetResult(); does not wait for the Observables Finally since it's run on different thread.

Try running it multiple times: you will see that the Debug.WriteLine succeeds occasionally, at least on my Visual Studio setup.

Upvotes: 2

Related Questions