Amazing User
Amazing User

Reputation: 3563

How can I reverse DispatcherTimer?

I need to reverse DispatcherTimer

    public TestTimer()
    {
        Timer = new DispatcherTimer();
        Timer.Interval = new TimeSpan(0, 0, 1);
        Timer.Tick += TimerTick;
        StartTimer();            
    }

    private DispatcherTimer Timer;

    private int _seconds;
    public int Seconds
    {
        get { return _seconds; }
        set
        {
            if(value > -1 && value < 61)
                _seconds = value;
        }
    }

    private int _minutes;
    public int Minutes
    {
        get { return _minutes; }
        set
        {
            if (value > -1 && value < 61)
                _minutes = value;
        }
    }

    private int _hours;
    public int Hours
    {
        get { return _hours; }
        set
        {
            if (value > -1 && value < 25)
                _hours = value;
        }
    }

    public void StartTimer()
    {
        Timer.Start();
    }

    public void StopTimer()
    {
        Timer.Stop();
    }

    private void TimerTick(object sender, EventArgs e)
    {
        if (Seconds > 59)
        {
            Seconds = 0;
            Minutes++;

            if (Minutes > 59)
            {
                Minutes = 0;
                Hours++;

                if (Hours > 23)
                    Hours = 0;
            }
        }
        Seconds++;

        TimeFormat = string.Format("{0:00}:{1:00}:{2:00}",
            Hours, Minutes, Seconds);
    }

Upvotes: 1

Views: 204

Answers (1)

Slate
Slate

Reputation: 3694

I'm not sure what you're trying to do or for what purpose.

If you want counting then you can use the TimerTick event handler to elapse every second and to count, or more efficiently, use DateTime.Now when you start, and subtract DateTime.Now when finished to get a TimeSpan between them.

If you're counting down, you can use the same approach but subtract the elapsed number of ticks away from your start to get your current countdown value.

Upvotes: 2

Related Questions