bbrooke
bbrooke

Reputation: 2347

Stop/Exit Timer

I'm trying to exit out of the Timer below after 5 seconds has past.

I tried workoutTimer.Stop();but get the message that 'workoutTimer' does not exist in the context.

Any feedback is greatly appreciated.

var i = 0;
var workoutTimer = new Timer((o) => {
    Device.BeginInvokeOnMainThread(() => {
        i = i+1;
        if (if i == 5){
            Console.WriteLine('Exit Timer here');
            //workoutTimer.Stop();
        }
    });
}, null, 1000, 1000);

Upvotes: 2

Views: 512

Answers (1)

Iain Smith
Iain Smith

Reputation: 9703

have you tried calling Dispose() and creating a private Timer variable like this:

Timer workoutTimer;
void TimerMethod () {
    var i = 0;
    workoutTimer = new Timer (o =>  {Device.BeginInvokeOnMainThread (() =>  {
        i = i + 1;
        if (i == 5) {
            Console.WriteLine ("Exit Timer here");
            workoutTimer.Dispose ();
        }
        Console.WriteLine ("tick");
    });
    }, null, 1000, 1000);
}

Upvotes: 1

Related Questions