Solved

Stop Watch Question

Posted on 2006-10-30
4
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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