2

In my win forms application I get data from web service periodically or on user request. It is required, that the web service is queried no more often than every 2 seconds, whereas the timer period is 5 seconds. At the startup the app should also get the data immediately.

            IObservable<Timestamped<long>> timerObservable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(5))
                                                                    .Timestamp();
        IObservable<Timestamped<long>> buttonObservable = Observable.FromEvent(handler => button1.Click += handler, handler => button1.Click -= handler)
                                                                    .Select(ev => 0L)
                                                                    .Timestamp();

        _subscription = timerObservable.Merge(buttonObservable)/*some code probably should be here*/
                                        .Subscribe(l => Debug.WriteLine("send request to web service received at: " + DateTime.Now.TimeOfDay));

I can't achieve that with throttle, because it always delays sending the request. I tried also bufferWithTime like this:

.BufferWithTime(TimeSpan.FromSeconds(2))
.Where(l => l.Count > 0)

but it delays the startup. Any solution to that? I use RX 1.0 as the application is .NET 3.5 based.

Piotr Cierpich
  • 427
  • 2
  • 11
  • Do you want to queue button presses to generate value immediately after 2 sec timeout, if pressed during timeout? Should the 5 second timer reset after button press has generated a value? – supertopi Feb 04 '15 at 13:57
  • It would be perfect if the timer was reset once the button is pressed, but I don't know how to acomplish that either, so I could live with a solution where values are generated with a frequency no less than 2 seconds. Meaning that the value is pushed to subscriber immediately if time from last value generated is 2 seconds or more or get queued if it is less. Only the last value generated matters, so if there were few button clicks before 2 seconds timeout from previous value generation, they all could be regarded as one. – Piotr Cierpich Feb 04 '15 at 14:24
  • 3
    possible duplicate of [Rx: How can I respond immediately, and throttle subsequent requests](http://stackoverflow.com/questions/7999503/rx-how-can-i-respond-immediately-and-throttle-subsequent-requests) – James World Feb 04 '15 at 15:03
  • The question I've linked is old enough to sport 1.0 code and recently updated enough to have some nice revisions for those with later Rx versions. – James World Feb 04 '15 at 15:12

0 Answers0