none
Authenticated StopWatch problem RRS feed

  • Question

  • Hi Friends,

    Please I want to add stopwatch into my winform I am using following code but its very fast and running very fast seem not reliable I am request if some has code forward me please

     private void timer1_Tick(object sender, EventArgs e)
            {
                seconds++;
                micro++;
                if ( micro>9)
                {
                    seconds++;
                    micro = 0;

                }

                    if (seconds > 59)
                {
                    minutes++;
                    seconds = 0;
                }
                if (hours > 59)
                    {
                        hours++;
                        minutes = 0;
                    }
                  //  lblhours.Text = appendZero(hours);
                  //  lblminutes.Text = appendZero(minutes);
                  //  lblseconds.Text = appendZero(seconds);
                  //lblmicrosecond.Text= appendZero(micro);

            }

            private string appendZero(double str)
            {

                if (str <= 9)
                    return "0" + str;
                else
                    return str.ToString();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                timer1.Start();
            }


    asad

    Monday, September 9, 2019 2:36 PM

Answers

  • Hi malikasad,

    Thanks for the feedback.

    It works for me. You could download it to check if it works.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by malikasad Wednesday, September 18, 2019 3:27 AM
    Monday, September 16, 2019 8:34 AM
    Moderator

All replies

  • You didn't show us how you initialized Timer1, so we can't tell what interval you asked for.  That determines how fast you'll bump the times.

    I can't tell what you were trying to with "micro", but the net effect is that you'll bump the seconds count 11 times in every 10 seconds.

    One problem with counting the way you do is that the timer callbacks are not guaranteed to be precise. Any any errors will accumulate over time.  You might consider grabbing the system time when you start the timer, then in your timer callback, grab the current system time and subtract the starting time.  That will get you a much more accurate elapsed time.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.


    • Edited by Tim Roberts Monday, September 9, 2019 6:18 PM
    Monday, September 9, 2019 6:18 PM
  • Hi malikasad,

    Thank you for posting here.

    First, could you tell you what do what do you want to do about the code you provided?

    Second, what is related to your Authenticated StopWatch, could you describe it more clearly?

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 10, 2019 6:38 AM
    Moderator
  • thank you for reply, authenticated means its speed like standard stop watch, further in above code I was trying to create stopwatch by using timer

    asad

    Tuesday, September 10, 2019 5:15 PM
  • The fastest "stopwatch" available in .NET is the Stopwatch class. It uses the high resolution timer of Windows and is used to measure the speed of code at the fastest rate. 

    var watch = new Stopwatch();
    watch.Start();
    //Do something
    watch.Stop();
    
    var elapsedTime = watch.Elapsed;
    
    
    If you need to measure speed of a method then put the code around the method call. If you need to measure across multiple calls (or UI work) then make the variable a field and start and stop it in your UI handlers.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, September 10, 2019 7:42 PM
    Moderator
  • Hi mailasad,

    Thanks for the feedback.

    I make a simple code to make a stopwatch by using timer, you could have a look.

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private DateTime _start;
            private void timer1_Tick(object sender, EventArgs e)
            {
                TimeSpan duration = DateTime.Now - _start;
                label1.Text = duration.ToString();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                _start = DateTime.Now;
                timer1.Start();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                timer1.Stop();
                label1.Text = "";
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 11, 2019 6:45 AM
    Moderator
  • sir I tried the above code its not working

    thank you.

    regards,

    asad


    asad

    Thursday, September 12, 2019 10:51 AM
  • Hi mailkasad,

    Thanks for the feedback.

    >>sir I tried the above code its not working

    I want to know if you get the same result as the above picture I provided when you run this code.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 13, 2019 7:21 AM
    Moderator
  • please its not working with me 

    asad

    Saturday, September 14, 2019 10:00 AM
  • Hi malikasad,

    Thanks for the feedback.

    It works for me. You could download it to check if it works.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by malikasad Wednesday, September 18, 2019 3:27 AM
    Monday, September 16, 2019 8:34 AM
    Moderator