0

I want to create an Observable that emits items at random intervals.

I've found an answer, but it refers to Rx.Js, that I don't know how to translate to Rx.NET

How to create an infinite Observable that produces random numbers at random intervals?

How can I do it in .NET?

halfer
  • 19,824
  • 17
  • 99
  • 186
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • I think this was closed incorrectly. I'm not looking to delay the items of an **existing** observable, but to **create** one *infinite* observable that emits items at random delays. The answers provided here https://stackoverflow.com/questions/10071004/delay-function-to-postpone-iobservable-values-dynamically solve a different scenario. – SuperJMN Sep 17 '19 at 07:45
  • 1
    For those interested https://stackoverflow.com/questions/30733906/generate-numbers-at-random-time-intervals-with-rx is probably closer to the answer wanted and should be what it should be marked as Duplicate with. – Glenn Watson Sep 17 '19 at 08:43
  • @GlennWatson Exactly. I've tried that. You've also noticed for sure that that answer doesn't apply either, because generating an infinite sequence with that approach hangs the computer :) – SuperJMN Sep 17 '19 at 08:48
  • 2
    @SuperJMN `Observable.Generate` from the [answer](https://stackoverflow.com/a/30736875/958732) to the question linked by Glenn Watson does exactly what you want. `var random = new Random(); var infiniteRandomDelaySequence = Observable.Generate( initialState: 0, condition: x => true, iterate: x => random.Next(0, 100), resultSelector: x => x, timeSelector: x => TimeSpan.FromMilliseconds(random.Next(0, 1000)) ).TimeInterval().Subscribe(x => Console.WriteLine("{0} ({1})", x.Value, x.Interval))` If it hangs your program something else is wrong. – Johnbot Sep 17 '19 at 12:22
  • I think your thread being blocked issue is due to `Observable.Generate` being run on the Immediate scheduler. `Generate` has a parameter that takes an `IScheduler` which you can specify a scheduler that executes on a separate thread. I think the rest of that other question/answer solves your issue. – Glenn Watson Sep 18 '19 at 02:09
  • @GlennWatson Exactly! that was the issue! Thanks for helping again! – SuperJMN Sep 18 '19 at 22:30

0 Answers0