Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

system.reactive - Generate numbers at random time intervals with rx

How can I generate a list of number with Rx.Net, like 0-100, where each number is generated at a random time?

edit:

seems like this works

    public void NonBlocking_event_driven()
    {
        var random = new Random();
        var ob = Observable.Create<int>(
        observer =>
        {
            timer.Interval = 1000;
            timer.Elapsed += (s, e) => observer.OnNext(random.Next(0, 3));
            timer.Elapsed += OnTimerElapsed;
            timer.Start();
            return Disposable.Empty;
        });
        var subscription = ob.Subscribe(UserAction);
        Console.ReadLine();
        subscription.Dispose();
    }
    private void OnTimerElapsed(object sender, ElapsedEventArgs e)
    {
        var random = new Random();
        timer.Interval = random.Next(1, 10)*1000;
        Console.WriteLine(e.SignalTime);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's an operator for that, you can use Generate:

var source = Observable.Generate(
  0,
  x => x < 100,
  x => x + 1,
  _ => random.Next(0, 3),
  _ => TimeSpan.FromMilliseconds(random.Next(1, 10)* 1000));

source.Subscribe(x => Console.WriteLine(x));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...