[OTP] vb6 - how can i measure an elapsed time in milliseconds?
Locked
hi, im a hischool student and i'm doing my project for graduation in visual basic. The program is about a way to measure the reaction time. I want to measure that in milliseconds, but I was only able to measure the elapsed time in seconds. How can I measure the elapsed time in seconds!
a lot of thanx
rorro
Answers
DateTime is a synonym for the VB Date Datatype. There are the same and interchangible. However, for .NET 2005 (VB 2005) using the System.Diagnostics.Stopwatch structure would yield higher resolution timing than the Date type.
With regards to types not being defined you must ensure that the type you're using is qualified and/or in-scope. Technically both DateTime and Timespan are defined in the System namespace. Typically this namespace is imported by default but if for whatever reason it isn't you may fully qualify the type names as System.DateTime and System.Timespan. If for whatever reason you have some namespace also named System in your project you can further qualify them as Global.System.DateTime and Global.System.Timespan.
However, Date is a language keyword that evaluates the same ast the DateTime structure and is always in scope so you should use it when convenient. Regardless I still highly recommend the Stopwatch if you're using .NET 2.0 as it was made for this type of thing.
These forums are for VB.NET and there are better places to find answers for older versions of VB. Maybe the
VB6 newgroups - http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.vb.general.discussion&lang=en&cr=US
Or perhaps the VB6 resource center
http://msdn.microsoft.com/vbrun/or perhaps www.vbcity.com may be useful places to search for answers on VB6 related questions.
Hopefully this helps.
All Replies
The following should get you started:
Dim StartTime As DateTime = DateTime.Now()
... Do your processing here.
Dim EndTime As DateTime = DateTime.Now()
Dim Span As TimeSpan = EndTime - StartTime
Console.WriteLine(Span.TotalMilliseconds)
Hope this helps,
thank you!!!! I will try it and I will let you know if it works or not!

a lot of thanx,
Hi, first of all I want to thank you for trying to help me.
I tried and it didn´t work. the dim as DATETIME does not exist, and the dim span as timespan neither. What else can I do???
Thanx again,
DateTime is a synonym for the VB Date Datatype. There are the same and interchangible. However, for .NET 2005 (VB 2005) using the System.Diagnostics.Stopwatch structure would yield higher resolution timing than the Date type.
With regards to types not being defined you must ensure that the type you're using is qualified and/or in-scope. Technically both DateTime and Timespan are defined in the System namespace. Typically this namespace is imported by default but if for whatever reason it isn't you may fully qualify the type names as System.DateTime and System.Timespan. If for whatever reason you have some namespace also named System in your project you can further qualify them as Global.System.DateTime and Global.System.Timespan.
However, Date is a language keyword that evaluates the same ast the DateTime structure and is always in scope so you should use it when convenient. Regardless I still highly recommend the Stopwatch if you're using .NET 2.0 as it was made for this type of thing.
I tried the sample code, and it worked for me. You may want to explicitly import the System namespace before any declarations.
Ex:
Imports
System 'try this to resolve the DateTime, and TimeSpan issuesModule
Module1 'first declaration. Sub Main() Dim StartTime As DateTime = DateTime.Now() ' your process code here.Dim EndTime As DateTime = DateTime.Now()
Dim Span As TimeSpan = EndTime - StartTime
Console.WriteLine(Span.TotalMilliseconds)
End Sub
End Module
I think the original post was a question based upon Visual Basic 6.0 not VB.Net as the System namespace is allready included in all VB.Net projects based upon the Console Application and Windows Application templates.
For VB 6.0, you would need to use the Win32 API ... an example solution would be:
'****************************************************
'Copy the following into a single VB 6.0 mdule
'****************************************************
Option Explicit
Public Declare Function GetTickCount Lib "kernel32" () As Long
Public Sub Main()
Dim dtStart As Long
Dim dtEnd As Long
Dim result As Long
Dim i As Integer
dtStart = GetTickCount
For i = 0 To 10000
DoEvents
Next
dtEnd = GetTickCount
result = dtEnd - dtStart
MsgBox "Duration is " & result & " milliseconds."
End SubThank you SGBScourge! it was almost what I wanted. I´m going to detail more about my program. It is in VB 6.0. I have 2 command bottons, 1 timer and 1 shape. First you press cmdbotton 1, it makes that the timer begins. After a x time it apears the shape that was hidden. When the shape apears the timer get off. And you have to press cmdbotton 2 so you can see how fast did you react. The time elapsed between the shape apears and the cmdbotton 2 press, it's the one I want to measure. It has to be in milliseconds.
Hope someone can help me

Lots of thanx
For VB6 Help, look at any of the resources described in the following post:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=551512&SiteID=1
These forums are for VB.NET and there are better places to find answers for older versions of VB. Maybe the
VB6 newgroups - http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.vb.general.discussion&lang=en&cr=US
Or perhaps the VB6 resource center
http://msdn.microsoft.com/vbrun/or perhaps www.vbcity.com may be useful places to search for answers on VB6 related questions.
Hopefully this helps.


