ConsultingEasy
ConsultingEasy

Reputation: 385

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

Having a little problem with the windows form timer. Its a very basic question, but I've looked around and cannot seem to find the answer (I probably deserve a slap).

I need to be able to get the value of the timer, whether its elapsed time is greater than an interval of 500ms.

something like

Timer.Elapsed >= 500

Upvotes: 6

Views: 22279

Answers (8)

J_Goat
J_Goat

Reputation: 1

You can specify an integer that is assigned with 0 and increments with the timer's interval. Put it inside the timer's tick method so that you can indirectly get the time elapsed by that integer

Upvotes: 0

Tiago Almeida
Tiago Almeida

Reputation: 31

I came with a solution of mine which is kinda simple:

1 - Before starting the timer I store the current time in TotalMillisseconds (from the DateTime.Now.TimeOfDay.TotalMilliseconds):

double _startingTime = DateTime.Now.TimeOfDay.TotalMilliseconds;

2 - Everytime the timer ticks, I get the Current Time again, then I use a double variable to get the difference between these two:

double _currentTime = DateTime.Now.TimeOfDay.TotalMilliseconds;
double _elapsed = _currentTime - _startingTime;

if(_elapsed >= 500)
{
    MessageBox.Show("As you command, master!");
    _startingTime = _currentTime;
}

if(_currentTime < _startingTime)
    _startingTime = _currentTime;

3 - And finally, because the TotalMilliseconds will return the number of Milliseconds that have passed since 00:00 (12pm), that means that when it's midnight, the TotalMilliseconds will be equal to 0. In this case I just check if the _currentTime is lower than the _startingTime, and if so, set the _startingTime to the _currentTime, so that I can calculate again.

I hope this helps

Upvotes: 0

Andrew
Andrew

Reputation: 7880

Based on my discussion with David here about Stopwatch vs DateTime, I decided to post the two approaches (very simplified) for the cases in which you need to get the remaining time, so you can decide which one is better for you:

public partial class FormWithStopwatch : Form
{
    private readonly Stopwatch sw = new Stopwatch();
    // Auxiliary member to avoid doing TimeSpan.FromMilliseconds repeatedly
    private TimeSpan timerSpan;

    public void TimerStart()
    {
        timerSpan = TimeSpan.FromMilliseconds(timer.Interval);
        timer.Start();
        sw.Restart();
    }

    public TimeSpan GetRemaining()
    {
        return timerSpan - sw.Elapsed;
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        // Do your thing
        sw.Restart();
    }
}

public partial class FormWithDateTime : Form
{
    private DateTime timerEnd;

    public void TimerStart()
    {
        timerEnd = DateTime.Now.AddMilliseconds(timer.Interval);
        timer.Start();
    }

    public TimeSpan GetRemaining()
    {
        return timerEnd - DateTime.Now;
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        // Do your thing
        timerEnd = DateTime.Now.AddMilliseconds(timer.Interval);
    }
}

I honestly don't see a big benefit from using the Stopwatch. You actually need less lines by using a DateTime. Furthermore, the latter seems a tiny bit clearer to me.

Upvotes: 0

I wrote this quickly , might have some bugs but give you the general idea

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

Upvotes: 1

called2voyage
called2voyage

Reputation: 252

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

Upvotes: 2

JeremyK
JeremyK

Reputation: 1103

You can't do that with a Timer. Elapsed is the event that is triggered when it has reached 0.

If you want to listen to when the event has elapsed, register a listen to Elapsed. Interval is the member to set the time to wait.

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500675

Timer.Elapsed isn't a property returning "the elapsed time" - it's an event you subscribe to. The idea is that the event fires every so often.

It's not really clear whether you even want a Timer - perhaps System.Diagnostics.Stopwatch is really what you're after?

var stopwatch = Stopwatch.StartNew();
// Do some work here

if (stopwatch.ElapsedMilliseconds >= 500)
{
    ...
}

Upvotes: 18

David Heffernan
David Heffernan

Reputation: 612993

I need to be able to get the value of the timer, whether its elapsed time is greater than an interval of 500ms.

A timer does not provide an interface that allows you to ascertain how much time has elapsed. The only thing that they do is fire an event when they expire.

You need to record the passage of time using some other mechanism, for example the Stopwatch class.

Upvotes: 4

Related Questions