Private/Pacemaker.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Pacemaker
{
    $stopwatch = $null
    $intervalMilliseconds = 0

    Pacemaker($intervalMilliseconds)
    {
        $this.stopwatch = [System.Diagnostics.Stopwatch]::new()
        $this.intervalMilliseconds = $intervalMilliseconds
    }

    [void] Tick()
    {
        if ($this.stopwatch.IsRunning)
        {
            $this.stopwatch.Stop()
            $waitMilliseconds = $this.intervalMilliseconds - $this.stopwatch.ElapsedMilliseconds
            $this.stopwatch.Reset()
            if ($waitMilliseconds -gt 0)
            {
                Start-Sleep -Milliseconds $waitMilliseconds
            }
        }
        $this.stopwatch.Start()
    }
}