Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

Timer

Am I doing timer correctly? When I click Start, Milliseconds seems not run as fast. It runs like 1, 15, 35, 65 ,etc. It does not start from 1, 2, 3 (real fast here)
 DispatcherTimer tmr = new DispatcherTimer();
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            tmr.Interval = TimeSpan.FromSeconds(1);
            tmr.Tick += onTimerTick;
            tmr.Start();

void onTimerTick(object sender, EventArgs args)
        {
            textBox1.Text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}",
                  sw.Elapsed.Hours,
                  sw.Elapsed.Minutes,
                  sw.Elapsed.Seconds,
                  sw.Elapsed.Milliseconds);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            tmr.Stop();
            sw.Reset();
        }
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Typically anything less than 50 ms in .Net is going to cause problems...

What is the "big picture" here?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just one point to make certain you understand.
The values returned from the stopwatch (elapsed property) should be accurate to milliseconds - for normal usage a timer as you are using it is also good enough.