Solved

Stop Watch Question

Posted on 2006-10-30
4
208 Views
Last Modified: 2010-04-23
I am using the below code as a stopwatch.  Is it possible to adapt the code to be more accurate? Currently it displays results as: hours, minutes, and seconds.  I need to also display smaller increments such as tenths of a second. So the desired result includes the original result as well as the tenths of a second as well.

Thank you for your time,
Gr8life


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False

        Dim startDT As DateTime = DateTime.Now

        Dim stopDT As DateTime = DateTime.Now

        Dim elapsedTS As TimeSpan = stopDT.Subtract(startDT)
        Dim msg As String = elapsedTS.Hours & "h" & elapsedTS.Minutes & "m" & elapsedTS.Seconds & "s"
        MessageBox.Show("Process duration: " & msg)

        Button1.Enabled = True
    End Sub
0
Comment
Question by:gr8life
4 Comments
 
LVL 10

Assisted Solution

by:gangwisch
gangwisch earned 25 total points
ID: 17837447
TimeSpan.TotalMilliseconds should get you what you need
0
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 400 total points
ID: 17837489
Well, ellapsedTS.Milliseconds by itself is just the number of milliseconds; that's probably closer to what you want.  

Something like:
Dim msg As String = elapsedTS.Hours & "h" & elapsedTS.Minutes & "m" & elapsedTS.Seconds & "s" & elapsedTS.Milliseconds & "ms"

Unfortunately there are no units between "seconds" and "milliseconds" available in convenient properties, so if you actually want "tenths of a second" you'll have to do some division.  Of course, that's not really the end of the world.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 75 total points
ID: 17837759
Hi gr8life;

If you are using VS 2005 you can use the Stopwatch class

        Dim MyStopwatch As New Stopwatch
        MyStopwatch.Reset()
        MyStopwatch.Start()


        ' Code or function to be timed

        MyStopwatch.Stop()

        Dim msg As String = MyStopwatch.Elapsed.Hours & "-h " & _
            MyStopwatch.Elapsed.Minutes & "-m " & MyStopwatch.Elapsed.Seconds & _
            "-s " & MyStopwatch.Elapsed.Milliseconds & "-ms"
        MessageBox.Show("Process duration: " & msg)

Fernando
0
 

Author Comment

by:gr8life
ID: 17838997
Gangwisch, I was looking for more along the line of what TVoteyDisciple posted.  Thank you for your time.

TVoteyDisciple your answer is close enough.
 
Fernando, thank you for your response, but unfortunately I still have 2003.  Hopefully someone will read this post and be able to use your valuable code.

thanks everyone,
Gr8life
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question