Solved

Stop Watch Question

Posted on 2006-10-30
4
207 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 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

15 Experts available now in Live!

Get 1:1 Help Now