Using a Timer

Hello,

Here's my problem, i'll write it in text code:

now time = 18.00
turn on alarm = 20:00

start_program();

check_time()
if (now time = time to turn on alarm) --> turn on alarm
else if(now time = 30min to turn on alarm) --> make a sound
else if(now time = 15min to turn on alarm) --> make a sound
else if(now time = 5 min to turn on alarm) --> make a sound

So I kind of want a timer that know what time it is, so when its 30 min left until the alarm shall be switch on, it'll make a sound.

Is this possible

 

ANd one other thing, how can I say that a timer shall count down from 30 to 0, and when it reaches 0 it shall do something

THANX:)

BTW: I'm using c# and visual stuido...The latest .Net Framework. And i'm using Windows application




Answer this question

Using a Timer

  • msatish

    Here is my example. This code assumes that the form has a Timer named _timer with its interval set to 1000, and a label named _timeLabel.

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace WindowsApplication1 {

    public partial class MainForm : Form {

    //go off in 30 minutes and five seconds

    private DateTime _alarmTime = DateTime.Now + new TimeSpan(0, 30, 5);

    private bool _thirtyMinuteWarningSounded = false;

    private bool _fifteenMinuteWarningSounded = false;

    private bool _fiveMinuteWarningSounded = false;

    private bool _alarmSounded = false;

    public MainForm() {

    InitializeComponent();

    _timer.Start();

    }

    private void _timer_Tick(object sender, EventArgs e) {

    DateTime now = DateTime.Now;

    _timeLabel.Text = now.ToString();

    if(!_thirtyMinuteWarningSounded && now >= _alarmTime - new TimeSpan(0, 30, 0)) {

    Console.WriteLine("30 minute warning!");

    _thirtyMinuteWarningSounded = true;

    }

    if(!_fifteenMinuteWarningSounded && now >= _alarmTime - new TimeSpan(0, 15, 0)) {

    Console.WriteLine("15 minute warning!");

    _fifteenMinuteWarningSounded = true;

    }

    if(!_fiveMinuteWarningSounded && now >= _alarmTime - new TimeSpan(0, 5, 0)) {

    Console.WriteLine("5 minute warning!");

    _fiveMinuteWarningSounded = true;

    }

    if(!_alarmSounded && now >= _alarmTime) {

    Console.WriteLine("alarm!");

    _alarmSounded = true;

    _timer.Stop();

    }

    }

    }

    }


  • dariusj18

    Hey :)

    Don't be sorry :) Every answer was helping me to reach my goal.

    This is the code I ended up with. But there's one problem, when the timer has counted down, it ends. But I want it to start all over again. Because you have to turn of the alarm too :) And on/off again several times:)

    And if anyone has a notes to my code I'm glad to hear about it :) Just to say so: In my code I've got a timer called: timer1 and a label called label_interval. And I've used 5 minutes and down to test it, so I wouldn't be sitting all day waiting :P

    When the timer reach zero, it shall turn on the alarm (that's okey) and change the "DateTime dt" to a new time when it's time for the alarm to go off. And start over again..Can I just call a new methode and in this methode I'll do those things, and then call TimerTing() over again

    DateTime dt;

    TimeSpan ts;

    bool lessThanOneMin = false;

    bool lessThanTenMin = false;

    bool lessThanFitheenMin = false;

    bool lessThanThirtyMin = false;

    bool lessThanOneHour = false;

    bool done;

    public Form1()

    {

    InitializeComponent();

    dt = new DateTime(2006, 5, 26, 19, 35, 0);

    timer1.Interval = 30 * 1000;

    label_interval.Text = "Intervall: " + (string.Format("{0}", timer1.Interval / 1000)) + " sekunder";

    }

    private void TimerTing()

    {

    ts = dt - DateTime.Now;

    if (ts.Minutes <= 5 && !lessThanOneHour)

    {

    lessThanOneHour = true;

    timer1.Interval = 30 * 1000;

    label_interval.Text = "Intervall: 30 sekunder";

    MessageBox.Show("<=5");

    }

    else if (ts.Minutes <= 4 && !lessThanThirtyMin)

    {

    lessThanThirtyMin = true;

    timer1.Interval = 20 * 1000;

    label_interval.Text = "Intervall: 20 sekunder";

    MessageBox.Show("<=4");

    }

    else if (ts.Minutes <= 3 && !lessThanFitheenMin)

    {

    lessThanFitheenMin = true;

    timer1.Interval = 10 * 1000;

    label_interval.Text = "Intervall: 10 sekunder";

    MessageBox.Show("<=3");

    }

    else if (ts.Minutes <= 2 && !lessThanTenMin)

    {

    lessThanTenMin = true;

    timer1.Interval = 5 * 1000;

    label_interval.Text = "Intervall: 5 sekunder";

    MessageBox.Show("<=2");

    }

    if (ts.Minutes <= 1 && !lessThanOneMin)

    {

    lessThanOneMin = true;

    timer1.Interval = 1 * 1000;

    label_interval.Text = "Intervall: 1 sekund";

    MessageBox.Show("<=1");

    }

    }

    private void timer1_Tick(object sender, EventArgs e)

    {

    TimerTing();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    TimerTing();

    timer1.Start();

    }



  • KarthikReddy

    Hi

    this snippet will able to perfom your task using a timer like :-

  • Stephen McCloskey

    Hey :)

    Don't be sorry :) Every answer was helping me to reach my goal.
    Thanx everyone for taking your time to give me an answer :) I really appriciate that :)

    This is the code I ended up with. But there's one problem, when the timer has counted down, it ends. But I want it to start all over again. Because you have to turn of the alarm too :) And on/off again several times:)

    And if anyone has a notes to my code I'm glad to hear about it :) Just to say so: In my code I've got a timer called: timer1 and a label called label_interval. And I've used 5 minutes and down to test it, so I wouldn't be sitting all day waiting :P

    When the timer reach zero, it shall turn on the alarm (that's okey) and change the "DateTime dt" to a new time when it's time for the alarm to go off. And start over again..Can I just call a new methode and in this methode I'll do those things, and then call TimerTing() over again

     

    DateTime dt;

    TimeSpan ts;

    bool lessThanOneMin = false;

    bool lessThanTenMin = false;

    bool lessThanFitheenMin = false;

    bool lessThanThirtyMin = false;

    bool lessThanOneHour = false;

    bool done;

    public Form1()

    {

    InitializeComponent();

    dt = new DateTime(2006, 5, 26, 19, 35, 0);

    timer1.Interval = 30 * 1000;

    label_interval.Text = "Intervall: " + (string.Format("{0}", timer1.Interval / 1000)) + " sekunder";

    }

    private void TimerTing()

    {

    ts = dt - DateTime.Now;

    if (ts.Minutes <= 5 && !lessThanOneHour)

    {

    lessThanOneHour = true;

    timer1.Interval = 30 * 1000;

    label_interval.Text = "Intervall: 30 sekunder";

    MessageBox.Show("<=5");

    }

    else if (ts.Minutes <= 4 && !lessThanThirtyMin)

    {

    lessThanThirtyMin = true;

    timer1.Interval = 20 * 1000;

    label_interval.Text = "Intervall: 20 sekunder";

    MessageBox.Show("<=4");

    }

    else if (ts.Minutes <= 3 && !lessThanFitheenMin)

    {

    lessThanFitheenMin = true;

    timer1.Interval = 10 * 1000;

    label_interval.Text = "Intervall: 10 sekunder";

    MessageBox.Show("<=3");

    }

    else if (ts.Minutes <= 2 && !lessThanTenMin)

    {

    lessThanTenMin = true;

    timer1.Interval = 5 * 1000;

    label_interval.Text = "Intervall: 5 sekunder";

    MessageBox.Show("<=2");

    }

    if (ts.Minutes <= 1 && !lessThanOneMin)

    {

    lessThanOneMin = true;

    timer1.Interval = 1 * 1000;

    label_interval.Text = "Intervall: 1 sekund";

    MessageBox.Show("<=1");

    }

    }

    private void timer1_Tick(object sender, EventArgs e)

    {

    TimerTing();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    TimerTing();

    timer1.Start();

    }



  • Chuck Berg

    Use the System.Windows.Forms.Timer or System.Threading.Timer class to set up a method to be called at a certain time in the future. You could either use one timer for each time you want to make the sound (so you can specify different intervals for each), or you could simply reset the timer's interval each time it goes off at the right time (e.g., when the 30 min timer goes off, reset the timer's interval to 15 minutes).
  • Mathiarasi

    Hi, Im sorry if my solution is not perfect. I was trying to help.

  • phillihp

    Sibusiso wrote:
    Hi, Im sorry if my solution is not perfect. I was trying to help.

    Nah, don't apologize. You were being helpful, and I was just too lazy to make an example the first time around anyway. But, I felt this post deserved a cleaner example, as its always good to encourage others to write as clean of code as reasonably possible.


  • JaceMan

    While I appreciate Sibusiso going through the trouble of making a full-blown code sample, it has several problems that could cause it to not behave correctly.  For one, there is no guarantee that the timer will be fired exactly every second, just approximately every second, and when testing the current time, there may be some seconds that are missed.  For example, if one of the alarms is supposed to sound at 12:00:00, the timer event may fire at 11:59:59 and twice at 12:00:01, but never at 12:00:00, and so the alarm would never fire.  A better approach would be to test whether the current time is greater than or equal to the time when the alaram is to sound.  Once the time has been reached or exceeded, sound the alarm and then set a flag so to prevent the alarm from sounding the next time around.

    Plus, each time DateTime.Now is called, it calculates and returns the current time, which may have changed since the last time it was called (even if it was only the line before).  So, the code:

       int hour = DateTime.Now.Hour;

       int min = DateTime.Now.Minute;

       int sec = DateTime.Now.Second;

     

    may return inconsistent results.  The time when the first line is executed could be 11:59:59.999.  So the hour variable would be set to 11.  But, when the next two lines are executed the time cuold be 12:00:00.  And so the minute and second variables would each be set to 0.  Put these variables together, and you get 11:00:00, which is way off.  A more correct solution would be:

     

    DateTime now = DateTime.Now;

       int hour = now.Hour;

       int min = now.Minute;

       int sec = now.Second;

     

    However, there is really no need to seperate the hour, minute, and seconds into seperate variables at this point anyway.

     

    Lastly, I would discourage creating your own time formatting function.  The DateTime.ToString() function has all sorts of formatting options, and there really isn't any reason not to use it.  Plus, it has the added benefit of applying the formatting rules of your current culture, depending upon which format you specify.


  • Using a Timer