Get the value of System.Windows.Forms.Timer?

Has a slight problem with the window timer. This is a very simple question, but I looked around and cannot find the answer (I probably deserve a slap in the face).

I need to get the timer value if its elapsed time is longer than the 500 ms interval.

sort of

Timer.Elapsed >= 500 
+4
source share
5 answers

Set the timer Interval property to the number of milliseconds you want to enable (500 in your example) and add an event handler for the Tick event.

+2
source

Timer.Elapsed not a property that returns "elapsed time" - it is an event that you are subscribed to. The idea is that the event fires so often.

It's not entirely clear if you even want a Timer - perhaps System.Diagnostics.Stopwatch are you after?

 var stopwatch = Stopwatch.StartNew(); // Do some work here if (stopwatch.ElapsedMilliseconds >= 500) { ... } 
+12
source

I need to get the timer value if its elapsed time is longer than the 500 ms interval.

The timer does not provide an interface that allows you to determine how much time has passed. The only thing they do is fire events when they expire.

You need to record the time using some other mechanism, such as the Stopwatch class.

+4
source

You cannot do this with Timer . Elapsed is an event that fires when it reaches 0.

If you want to listen when an event has passed, register an Elapsed listen. Interval is a member to set the timeout.

See here: http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.100).aspx

0
source

I wrote it quickly, maybe there are some errors, but I give you a general idea

 Timer timer = new System.Windows.Forms.Timer(); timer.Interval = 500; timer.Elapsed += (s,a) => { MyFunction(); timer.Stop(); } timer.Start(); 
0
source

All Articles