CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2012
    Posts
    46

    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?

  2. #2
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Compare double value of stopwatch

    Quote 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

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    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.

  4. #4
    Join Date
    Feb 2012
    Posts
    46

    Re: Compare double value of stopwatch

    It seems not to be working either, please help!

  5. #5
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    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

  6. #6
    Join Date
    Feb 2012
    Posts
    46

    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.

  7. #7
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    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

  8. #8
    Join Date
    Feb 2012
    Posts
    46

    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!

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    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
  •  





Click Here to Expand Forum to Full Width

Featured