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

Using a Timer
msatish
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
Mathiarasi
phillihp
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.