简体   繁体   中英

Rx take first element and skip following elements for an interval of time

I try to access a stream every 2 seconds and get the first value within this time. Example:

Values: -1--2-3---4-----5--6-7--8
Result: -1-------3--------5------

I tried the code like int this thread (quite similar problem):

subject.AsObservable().Window(TimeSpan.FromSeconds(2))
            .SelectMany(f => f.Take(1))
            .Subscribe(f =>
        {
            Console.WriteLine("Counter: " + counter + " Time:" + DateTime.Now.Millisecond);
            counter++;
        });

However, the 2 seconds do not work, the counter updates too fast even after 200 milliseconds.

What am I missing?

Data are added to the subject by using this code:

Task.Run(async () =>
            {
                while (await call.ResponseStream.MoveNext(default(CancellationToken)))
                {           
                    foreach (var result in call.ResponseStream.Current.Results)
                    {
                        subject.OnNext(result);
                    }
                }
            });

Your query seems to be perfectly fine. Let's test this by giving it a good set of source data:

var rnd = new Random();
var source =
    Observable
        .Generate(0, x => true, x => x + 1, x => x,
            x => TimeSpan.FromSeconds(rnd.NextDouble() / 10.0));

This is going to produce a value every 0.0 to 100.0 milliseconds. So if the query is correct we should expect to see a value produced within 100 milliseconds of each 2,0 second window (give or take Windows OS timing issues).

Here's a slightly better version of the subscription of the query:

var sw = Stopwatch.StartNew();
source
    .Window(TimeSpan.FromSeconds(2.0))
    .SelectMany(f => f.Take(1))
    .Subscribe(f =>
    {
        Console.WriteLine($"Counter: {f} Time: {sw.Elapsed.TotalMilliseconds}");
    });

These results I get are like:

Counter: 0 Time: 110.8073
Counter: 33 Time: 2124.7605
Counter: 67 Time: 4061.8636
Counter: 101 Time: 6061.1922
Counter: 134 Time: 8090.158
Counter: 169 Time: 10173.0396
Counter: 197 Time: 12153.0229
Counter: 233 Time: 14138.7718
Counter: 265 Time: 16144.8861
Counter: 296 Time: 18122.042
Counter: 337 Time: 20141.1837
Counter: 373 Time: 22115.0944
Counter: 410 Time: 24162.0706

It has a bit of timer drift, but it's following the expected pattern. The query works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM