Stop Watch Question

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,

    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
Who is Participating?
VoteyDiscipleConnect With a Mentor Commented:
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.
gangwischConnect With a Mentor Commented:
TimeSpan.TotalMilliseconds should get you what you need
Fernando SotoConnect With a Mentor RetiredCommented:
Hi gr8life;

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

        Dim MyStopwatch As New Stopwatch

        ' Code or function to be timed


        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)

gr8lifeAuthor Commented:
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,
All Courses

From novice to tech pro — start learning today.