添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
打酱油的菠萝  ·  Boost - 从Coroutine2 ...·  4 月前    · 
狂野的丝瓜  ·  mybatis ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to add a time interval to this Observable sequence( That is produce an integer sequence at a specific timespan) but it seems not to be working. When i remove the time, then it works time. Am i applying the timer wrongly?

var timer = Observable.Interval(TimeSpan.FromSeconds(2)).Take(4);
var nums = Observable.Range(1,1200).Where(a => a % 2 == 0);
var sourcenumbs = timer.SelectMany(nums);
var results = sourcenumbs.Subscribe(
              x => Console.WriteLine("OnNext: {0}",x),
              ex => Console.WriteLine("OnError: {0}",ex.Message),
              () => Console.WriteLine("OnComplete")

This code displays no output, Does it get Dispose before it reaches the Subscribe?

But if i had a forloop with a timer in it then it works. Why?

for (int i = 0; i < 10; i++)
     Thread.Sleep(TimeSpan.FromSeconds(0.9));
                What do you mean by "add a time interval to this Observable sequence". What is output you are expecting? How is the for loop even related to the observable sequence?
– Euphoric
                Apr 13, 2017 at 12:06
                @Euphoric I want the even numbers from 1, 1200 every 2 seconds, without the Observable.Interval it was working fine but i wanted to add a timer to it.
– Seek Addo
                Apr 13, 2017 at 12:09
private static async void Execute()
    var intervals = Observable.Interval(TimeSpan.FromSeconds(2)).StartWith(0);
    var evenNumbers = Enumerable.Range(1, 1200).Where(a => a % 2 == 0);
    var evenNumbersAtIntervals = intervals.Zip(evenNumbers, (_, num) => num);
        await evenNumbersAtIntervals.ForEachAsync(
            x => Console.WriteLine("OnNext: {0}", x)
        Console.WriteLine("Complete");
    catch(Exception e)
        Console.WriteLine("Exception " + e);

Take note that numbers are Enumerable and not Observable.

@SeekAddo You have to stop console from closing, as the code runs asynchronously. Simplest way is to Console.ReadKey(). See my edit. – Euphoric Apr 13, 2017 at 12:18 you were write. I am using Rider so the console doesn't close like in visual sutdio, but with the Console.ReadKey() it helped. – Seek Addo Apr 13, 2017 at 12:20 What about using await on the Subscriber, or the only way is to use the Console.ReadKey. I mean what if is in a real application and it has to send the output to another data. – Seek Addo Apr 13, 2017 at 12:22 @SeekAddo If you want to await completion of the sequence, you should use ForEachAsync instead of Subsribe. See edit. – Euphoric Apr 13, 2017 at 12:27

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.