|
-
May 24th, 2012, 05:24 AM
#1
Compare double value of stopwatch
Code:
private void comboBoxItemsforAlarm_SelectedIndexChanged(object sender, EventArgs e)
{
stopWatch.Restart();
double doubleStopWatch = Convert.ToDouble(stopWatch.Elapsed.Hours + "." + stopWatch.Elapsed.Minutes);
double timeAlarm = Convert.ToDouble(maskedTime.Text.ToString());
if (doubleStopWatch == timeAlarm)
{
mediaSoundPlayer.Stream = Properties.Resources.Song_of_Song; ;
mediaSoundPlayer.PlayLooping();
}
}
please i try to compare a stopwatch converted to a double value and a value from a textbox converted to a double value it seems not to do the comparison because it does not play the file when both are equal. what should i do?
-
May 24th, 2012, 06:01 AM
#2
Re: Compare double value of stopwatch
 Originally Posted by MSDN
Stopwatch.Restart Method
Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time.
Just read that from MSDN
-
May 24th, 2012, 06:16 AM
#3
Re: Compare double value of stopwatch
In addition to what Erendar said, comparing doubles for equality is almost never the correct thing to do. Will your stopwatch ever be exactly the same as the value in the text box? This is extremely unlikely.
Change the code to this:
Code:
if (doubleStopWatch > timeAlarm)
to trigger the alarm once the appropriate time has elapsed.
-
May 24th, 2012, 07:48 AM
#4
Re: Compare double value of stopwatch
It seems not to be working either, please help!
-
May 24th, 2012, 07:52 AM
#5
Re: Compare double value of stopwatch
Yeah because of what I said earlier. You actually calls reset BEFORE storing elapsed time so doubleStopWatch will always be nearly 0 (just the time for getting the value in memory). Just reverse the order of the two lines.
stopWatch.Restart(); means
stopWatch.Elapsed.Hours = 0
stopWatch.Elapsed.Minutes = 0
-
May 24th, 2012, 08:00 AM
#6
Re: Compare double value of stopwatch
I really appreciate your contributions, but could not still figure it out.
Erendar, please can you just add the code so that I can understand fully what you are saying. Thanks. go bless in Jesus name.
-
May 24th, 2012, 08:41 AM
#7
Re: Compare double value of stopwatch
Code:
private void comboBoxItemsforAlarm_SelectedIndexChanged(object sender, EventArgs e)
{
// We get current elapsed time of the stopwatch
double doubleStopWatch = Convert.ToDouble(stopWatch.Elapsed.Hours + "." + stopWatch.Elapsed.Minutes);
double timeAlarm = Convert.ToDouble(maskedTime.Text.ToString());
// Here you reset the stopwatch, so values that were in stopWatch.Elapsed.Hours are lost
stopWatch.Restart();
// As we've stored the values we can compare them to the timealarm
// Alarm should pops when we're on the time or after (the function may be called after
// the user-entered time because of all things that Windows have to process). Being
// on the exact value would be like winning at lottery
if (doubleStopWatch >= timeAlarm)
{
mediaSoundPlayer.Stream = Properties.Resources.Song_of_Song; ;
mediaSoundPlayer.PlayLooping();
// Shouldn't the reset be here ? I don't know what your program have to do,
// but at the moment you are resetting your timer regardless if it was above the
// user value or not
}
}
Last edited by Erendar; May 24th, 2012 at 08:45 AM.
Reason: formatting
-
May 24th, 2012, 09:48 AM
#8
Re: Compare double value of stopwatch
Code:
private void comboBoxItemsforAlarm_SelectedIndexChanged(object sender, EventArgs e)
{
//stopWatch.Start();
double doubleStopWatch = Convert.ToDouble(stopWatch.Elapsed.Hours + "." + stopWatch.Elapsed.Minutes);
double timeAlarm = Convert.ToDouble(maskedTime.Text);
stopWatch.Restart();
if (doubleStopWatch >= timeAlarm)
{
mediaSoundPlayer.Stream = Properties.Resources.Song_of_Song; ;
mediaSoundPlayer.PlayLooping();
}
}
I think i am getting there bit by bit. What it does now is that it only start playing when i reselect in the combobox which is not what I want. I am working on alarm. the maskedTime.Text is the input for the time to alarm (or time for the song to start playing) the stopwatch is to count until the time is met, and once it's met the event should fire to alert me.
for example if i input :0.01 which means one minute, i want the song to start playing once the one minute elapses. that is just it. Thanks!
-
May 24th, 2012, 03:05 PM
#9
Re: Compare double value of stopwatch
Next, stop testing equality between floating point numbers like that (in general). What every computer scientist should know about floating point numbers
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|