tags:

views:

60

answers:

1

I want to ask a simple question how to stop this timer.
Somebody's code is like this :


    let mutable timer = new DispatcherTimer()
    timer.Interval - new TimeSpan(0,0,0,0,100)
    timer.Start()

and I want to add a function that cause the timer to stop whenever I click button
So I put a code like this (I don't really have a clue) :


btnStop.Click
        |> Event.add(fun args -> timer.Stop())

but it says that it cannot be captured by closure
any idea how to do this?
sorry. I am not an experienced functional programmer. I just want to modify a code.

+4  A: 

Is there a reason to make the timer mutable? If you're never assigning a new value to it, then you can just drop mutable and it should work.

Otherwise, you'll need to use a reference cell instead of a normal mutable binding:

let timer = ref (new DispatcherTimer())
(!timer).Interval <- new TimeSpan(0,0,0,0,100)
(!timer).Start()

btnStop.Click
    |> Event.add(fun args -> (!timer).Stop())
kvb