Solved

Stop Watch Question

Posted on 2006-10-30
4
206 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
Comment Utility
TimeSpan.TotalMilliseconds should get you what you need
0
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 400 total points
Comment Utility
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 75 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now