Leon Barkan
Leon Barkan

Reputation: 2703

How to increment running timer

In my application I define a timer and set the interval for x seconds. After that when a 'click' event occurs I want to increment the running timer for y more seconds.

How can I do this?

The code sample:

private static Timer sTimer = new Timer();
sTimer.Interval = 50000;

When the event is invoked I want to do something like that:

sTimer.Interval = timeLeftInms + 5000;

Upvotes: 0

Views: 1580

Answers (3)

Igor Meszaros
Igor Meszaros

Reputation: 2127

You should create a custom Timer, which inherits from System.Timers.

Have a private Stopwatch stopWatch = new Stopwatch(); private member.

On Timer.Start: stopWatch.Start();

On Timer.Tick: stopWatch.Reset();

On

Timer.Interval 
          set {
                   _timer.Interval = _timer.Interval - 
                    stopwatch.Elapsed.Miliseconds + value;
              }

Upvotes: 3

P.Hup
P.Hup

Reputation: 3

Just do

sTimer.Interval += 5000;

inside the click event

Upvotes: 0

DarkSquirrel42
DarkSquirrel42

Reputation: 10257

have a timer that updates your system/view/whatever in a regular interval like every 250ms

when your system starts populate a datetime to hold the value when the first event is going to happen ... like DateTime.Now + TimeSpan.FromSeconds(20)

update said datetime variable instead of your timer ...

time left is the difference between your variable and DateTime.Now

Upvotes: 2

Related Questions