Paul is right when he says this is a bad idea. You logically create a situation where actions in the queue can cause system resources. You can even find that it works on your computer, but does not work on the client computer. Available memory, 32- / 64-bit processor, etc. May affect the code.
However, it's easy to change the code so that it does what you want.
At first, however, the Timer
method will correctly schedule timer events until the observer finishes before the next scheduled event. If the observer has not finished, the timer will wait. Remember that observed timers are “cold” observed, so for each registered observer there is actually a new observed timer. This is a one-to-one relationship.
This behavior prevents unintentional blowing of the timer by your resources.
So, as you define the code, OnNext
is OnNext
called every 1000 milliseconds, not every 100.
Now, to allow 100 milliseconds to run the code on a schedule, do the following:
Observable .Timer(TimeSpan.Zero, TimeSpan.FromMilliseconds(100)) .Select(x => Scheduler.NewThread.Schedule(() => { count++; Thread.Sleep(TimeSpan.FromMilliseconds(1000)); })) .Subscribe(x => { });
Effectively, this code is an IObservable<IDisposable>
, where each one-time action is a scheduled action that lasts 1000 milliseconds.
In my tests, this performed well and increased the score correctly.
I tried to explode my resources and found that the timer setting starts once every millisecond, I quickly got a System.OutOfMemoryException
, but found that the code worked if I changed the setting every two milliseconds. However, this allowed the use of more than 500 MB of RAM, while the code worked and created about 500 new threads. Not really nice.
Be careful!