How to measure elapsed time in c# with StopWatch

by Emanuele 2/20/2008 9:54:00 AM

A StopWatch class can measure elapsed time for one interval.
We can use for measuring performance of the new code blocks or algorithms.

Use IsRunning method to determine the state of StopWatch object.
Use Start method to begin measuring elapsed time, and Stop method to stop measuring elapsed time.

        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            textBox1.Text = DateTime.Now.TimeOfDay.ToString();
            Application.DoEvents();

            // Perform a long process
            Thread.Sleep(34564);

            stopwatch.Stop();
            textBox2.Text = DateTime.Now.TimeOfDay.ToString();
            textBox3.Text = stopwatch.Elapsed.Milliseconds.ToString();
            Application.DoEvents();
        }

 

You can find an example application in the attached file.

Related posts



Powered by BlogEngine.NET 1.3.1.0
Theme by Emanuele Bartolesi

About the author

Name of author Emanuele Bartolesi
I'm a senior developer and project manager.

Contact me Contact me

Calendar

<<  December 2008  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in

Download Day 2008