Last active
April 26, 2017 10:27
-
-
Save franssu/aa110b22a0bbd6b2c734870b600847c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Listing 11-1. Creating a RandomTicker that defines, publishes, and triggers an event | |
open System | |
open System.Timers | |
type RandomTicker(approxInterval) = | |
let timer = new Timer() | |
let rnd = new System.Random(99) | |
let tickEvent = new Event<float> () | |
let chooseInterval() : float = | |
approxInterval + approxInterval / 4.0 - float(rnd.Next((int)(approxInterval / 2.0))) | |
do timer.Interval <- chooseInterval() | |
do timer.Elapsed.Add(fun args -> | |
let interval = chooseInterval() | |
tickEvent.Trigger interval; | |
timer.Interval <- interval) | |
member x.RandomTick = tickEvent.Publish | |
member x.Start() = timer.Start() | |
member x.Stop() = timer.Stop() | |
interface IDisposable with | |
member x.Dispose() = timer.Dispose() | |
let rt = new RandomTicker(1000.0);; | |
rt.RandomTick.Add(fun nextInterval -> printfn "Tick, next = %A" nextInterval);; | |
rt.Start();; | |
rt.Stop();; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment