Results 1 to 10 of 10

Thread: [RESOLVED] [2005] Stopwatch with Milliseconds.

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Resolved [RESOLVED] [2005] Stopwatch with Milliseconds.

    Hi All,

    I'm trying to create a Stopwatch that starts with Milliseconds.
    I can already run a Clock with milliseconds but I want to start from zero like a stopwatch.
    Here's what I have for a running clock.

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         TextBox1.Text = "00:00:00:000"
    3.         TextBox2.Text = "00:00:00:000"
    4.     End Sub
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Timer1.Start()
    8.     End Sub
    9.  
    10.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    11.         Dim upTime As TimeSpan = TimeSpan.FromMilliseconds(Environment.TickCount)
    12.         TextBox1.Text = (Date.Now.ToString("hh:mm:ss:fff"))
    13.         TextBox2.Text =  ' what next, to create a stopwatch with Milliseconds
    14.     End Sub

    Thanks in advance,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Stopwatch with Milliseconds.

    So you're trying to update the display every millisecond?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Stopwatch with Milliseconds.

    Quote Originally Posted by jmcilhinney
    So you're trying to update the display every millisecond?
    Hi jm,

    Yes that's right.
    I want that it start with one millisecond then after the 1000 milli's we have 1 second then after 60 seconds we have one minute and so on.....

    Is that possible in the first place and can the Timer interval be set to 1

    Thanks in advance,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [2005] Stopwatch with Milliseconds.

    You won't get any real accuracy with the standard timer controls. You could try using the API if you are brave enough, accuracy is significantly better.
    I don't live here any more.

  5. #5

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Stopwatch with Milliseconds.

    Quote Originally Posted by wossname
    You won't get any real accuracy with the standard timer controls. You could try using the API if you are brave enough, accuracy is significantly better.
    Hi,

    Where can I find some good information about API.
    Or do you have an example how to use it.

    Thanks in advance,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Stopwatch with Milliseconds.

    Check out this thread. As I've said there, you don't actually need a Timer in order to calculate elapsed time, so its accuracy is not really a big issue. The Timer would only be used to display the elapsed time as it was elapsing. As soon as you stop the Stopwatch you can get the elapsed time with as much accuracy as a Button's Click event will allow. The only possible issues are whether a bit of stuttering in the display may occur and whether such a busy Timer will slow down other things. Give that solution a try and see what happens
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Stopwatch with Milliseconds.

    Hi jm,

    I've tryed the code but it isn't working!
    This is what I made of!
    What did I do wrong with it?


    VB Code:
    1. Dim myStopwatch As New Stopwatch
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         Timer1.Start()
    9.        
    10.     End Sub
    11.  
    12.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    13.         Dim elapsedTime As TimeSpan = Me.myStopwatch.Elapsed
    14.  
    15.         TextBox1.Text = String.Format("{0:d2}:{1:d2}:{2:d2}:{3:d2}", _
    16.                                        Convert.ToInt32(Math.Floor(elapsedTime.TotalHours)), _
    17.                                        elapsedTime.Minutes, _
    18.                                        elapsedTime.Seconds + Convert.ToInt32(Math.Round(elapsedTime.Milliseconds / 1000)))
    19.  
    20.  
    21.     End Sub

    Thanks,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  8. #8
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: [2005] Stopwatch with Milliseconds.

    This really isn't that hard to do, using the system time and the date subtract-method. You may need to adjust the code a little bit to conform it to your usage.
    VB Code:
    1. Private dtStartTime As Date
    2.  
    3. Private Sub Button1_Click(ByVal sender As System.Object, _
    4.     ByVal e As System.EventArgs) Handles Button1.Click
    5.  
    6.     'Button1 starts the stopwatch.
    7.     dtStartTime = Now
    8.     Timer1.Start()
    9.  
    10. End Sub
    11.  
    12. Private Sub Button2_Click(ByVal sender As System.Object, _
    13.     ByVal e As System.EventArgs) Handles Button2.Click
    14.  
    15.     'Button2 stops the stopwatch.
    16.     Timer1.Stop()
    17.  
    18. End Sub
    19.  
    20. Private Sub Timer1_Tick(ByVal sender As System.Object, _
    21.     ByVal e As System.EventArgs) Handles Timer1.Tick
    22.  
    23.     'An interval of 25ms for Timer1 works fine.
    24.     'Label1 displays the elapsed time.
    25.     Dim tsDiff As TimeSpan = Now.Subtract(dtStartTime)
    26.     Label1.Text = Format(Math.Floor(tsDiff.TotalHours), "00") & ":" & _
    27.         Format(tsDiff.Minutes, "00") & ":" & _
    28.         Format(tsDiff.Seconds, "00") & ":" & _
    29.         Format(tsDiff.Milliseconds, "000")
    30.  
    31. End Sub
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  9. #9

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Stopwatch with Milliseconds.

    Hi man,

    That does it, thanks.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] [2005] Stopwatch with Milliseconds.

    The code in post #7 lacks some fairly important code from my post in that other thread; specifically the line that starts the stopwatch. If you don't start it how can it measure anything? The advantage of using the Stopwatch class is that you don't have to worry about your own variables to store the time, plus you can pause it without having to do tedious calculations. I suggest that you use it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width