Scheduling a IEnumerable periodically with .NET reactive extensions

后端 未结 5 570
闹比i
闹比i 2021-01-24 05:21

Say for example I have an enumerable

dim e = Enumerable.Range(0, 1024)

I\'d like to be able to do

dim o = e.ToObservable(Timesp         


        
5条回答
  •  终归单人心
    2021-01-24 05:35

    You'd need something to schedule notifying observers with each value taken from the Enumerable. You can use the recursive Schedule overload on an Rx scheduler.

    Public Shared Function Schedule ( _
        scheduler As IScheduler, _
        dueTime As TimeSpan, _
        action As Action(Of Action(Of TimeSpan)) _
    ) As IDisposable
    

    On each scheduled invocation, simply call enumerator.MoveNext(), and call OnNext(enumerator.Current), and finally OnCompleted when MoveNext() returns false. This is pretty much the bare-bones way of doing it.

    An alternative was to express your requirement is to restate it as "for a sequence, have a minimum interval between each value".

    See this answer. The test case resembles your original question.

提交回复
热议问题