|
-
Jul 19th, 2006, 03:10 AM
#1
[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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "00:00:00:000"
TextBox2.Text = "00:00:00:000"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim upTime As TimeSpan = TimeSpan.FromMilliseconds(Environment.TickCount)
TextBox1.Text = (Date.Now.ToString("hh:mm:ss:fff"))
TextBox2.Text = ' what next, to create a stopwatch with Milliseconds
End Sub
Thanks in advance,
sparrow1
-
Jul 19th, 2006, 04:58 AM
#2
Re: [2005] Stopwatch with Milliseconds.
So you're trying to update the display every millisecond?
-
Jul 19th, 2006, 05:08 AM
#3
Re: [2005] Stopwatch with Milliseconds.
 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
-
Jul 19th, 2006, 06:50 AM
#4
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.
-
Jul 20th, 2006, 09:13 AM
#5
Re: [2005] Stopwatch with Milliseconds.
 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
-
Jul 20th, 2006, 04:25 PM
#6
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
-
Jul 24th, 2006, 08:46 AM
#7
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:
Dim myStopwatch As New Stopwatch
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsedTime As TimeSpan = Me.myStopwatch.Elapsed
TextBox1.Text = String.Format("{0:d2}:{1:d2}:{2:d2}:{3:d2}", _
Convert.ToInt32(Math.Floor(elapsedTime.TotalHours)), _
elapsedTime.Minutes, _
elapsedTime.Seconds + Convert.ToInt32(Math.Round(elapsedTime.Milliseconds / 1000)))
End Sub
Thanks,
sparrow1
-
Jul 24th, 2006, 09:36 AM
#8
Fanatic Member
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:
Private dtStartTime As Date
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'Button1 starts the stopwatch.
dtStartTime = Now
Timer1.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'Button2 stops the stopwatch.
Timer1.Stop()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
'An interval of 25ms for Timer1 works fine.
'Label1 displays the elapsed time.
Dim tsDiff As TimeSpan = Now.Subtract(dtStartTime)
Label1.Text = Format(Math.Floor(tsDiff.TotalHours), "00") & ":" & _
Format(tsDiff.Minutes, "00") & ":" & _
Format(tsDiff.Seconds, "00") & ":" & _
Format(tsDiff.Milliseconds, "000")
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!
-
Jul 24th, 2006, 09:56 AM
#9
Re: [2005] Stopwatch with Milliseconds.
Hi man,
That does it, thanks.
Wkr,
sparrow1
-
Jul 24th, 2006, 05:48 PM
#10
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|