Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate an event X numbers of times per second but not evenly spaced within each second
    primarykey
    data
    text
    <p>I'd like to write a class that will fire an event <code>x</code> a pre-defined numbers of times per second call it <code>n</code>.</p> <p>However, I would like <code>x</code> <strong>NOT</strong> to fire evenly spaced within each second.</p> <p>So, say <code>n</code> = 100, 25 might fire in the first 300ms then a further 50 over the next 600ms and the final 25 in the remaining 100ms.</p> <p>Ideally, I'd like better granularity then that presented above and a greater range of spacing within each second.</p> <p>I'm wondering if I would be able to create an array with defined millisecond and how many events to fire. Then use a loop and the <code>Stopwatch</code> class to determine whether the defined events for that millisecond waypoint should be fired.</p> <p>The question is, could the array be calculated for each second fast enough, each second should have random spacing.</p> <p>Obviously, the event would need to be Asynchronous to avoid getting delayed by what ever is wired up to it.</p> <p>Anyone come across any similar need?</p> <h2>UPDATE</h2> <p>I thought I'd at least put my initial effort on here.</p> <p>So I found that there is enough resolution to check which millisecond you are currently in and which second. Basically, each second I rebuild my spacing each millisecond gets an entry in the array and each entry says how many times to fire the event that millisecond.</p> <p>My issue is the spacing...I needs a better way to try and clump the event count, currently this jsut seems to plonk them in evenly spaced in either 0, 1 or 2.</p> <pre><code>public delegate void EmptyEventDelegate(); public class RandEvent { public event EmptyEventDelegate OnEvent = delegate { }; private bool running = false; Random r = new Random(); private int eventsPS; public RandEvent(int eventsPS = 1) { this.eventsPS = eventsPS; } public void Start() { running = true; Task.Factory.StartNew(() =&gt; { Run(); }); } private void Run() { var sw = new Stopwatch(); sw.Start(); int currentSecond = 0; int[] eventCount = BuildEventSpacing(); while(running) { if (currentSecond != sw.Elapsed.Seconds) { currentSecond = sw.Elapsed.Seconds; eventCount = BuildEventSpacing(); } else { for(int i = 0; i &lt; eventCount[sw.Elapsed.Milliseconds]; i++) OnEvent(); } } sw.Stop(); } private int[] BuildEventSpacing() { var array = new int[1000]; for (int i = 0; i &lt; eventsPS; i++) { array[r.Next(0, 999)]++; } return array; } public void Stop() { running = false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload