Calculating time since an event

spyderco

New member
Joined
Jun 14, 2005
Messages
2
Programming Experience
5-10
New to VB.net as of two days ago.

I am creating a Reaction Timer game where you click START button and then it changes the BG color. The user then clicks the STOP button as soon as they see the BG change and it calculates how long it took.

I was told to use a Timer class for this but all the timer classes seem to me counting uptil a specific point. I need my timer to continue endlessly until the STOP button is pressed.

I need this pretty accuarate, atleast to .01 seconds.

Any ideas of what to do? Please don't toss VB terminology at me or anything, I just started VB.net and come from a loose language (Perl).
 
I think you need this:
1. A timer which fires at every .01 second (100 if I am not mistaken)
2. A variable somewhere at form or module level acting as a counter

In START event, start the timer. Every time it fires, it records in the variable. In STOP event, stop timer, get the variable (counter value), do some calculation...

Alternatively, you can start by recording the microtime, and record another microtime in event of STOP. Compare the time...
 
add a timer control to the form and dim an integer at the module level

then in the timer's tick event simply:

intUserTime += 1

then when the user clicks start:
intUserTime = 0
Timer.Enabled = True

then when the stop button is clicked:
Timer.Enabled = False
and do the calculations you need on intUserTime

basically what ayozzhero said just slightly more detailed
 
I don't think you need a timer at all. Simply set a DateTime variable to DateTime.Now when you set the background colour. Then set a second DateTime variable to DateTime.Now when the user hits the Stop button. Use DateTime2.Subtract(DateTime1) to get the difference as a TimeSpan object. You can then use the members of the TimeSpan object to display the time taken however you like. Just make sure you note the difference between the properties that start with "Total" and those that don't. If a TimeSpan is 1 minute and 5 seconds, TimeSpan.Seconds will return 5 while TimeSpan.TotalSeconds will return 65.
 
Let's say you do not prefer a timer, an easier way is to calculate the interval using milisecond or nanosecond.

Put this somewhere at form level (not within the Sub):
Dim Counter As Long

At START put:
Counter = Environment.TickCount()

At STOP put:
MessageBox.Show(Environment.TickCount() - Counter)

It should display you the interval in miliseconds. Alternatively, you may want to use: System.DateTime.Now.Ticks
 
HI,

Thanks for your help.

VB.NET:
[size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] _totalSeconds [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer

[/color][/size][size=2]_totalSeconds = _stopTime.Subtract(_startTime)

[/size]

This is what I'm trying to do but it says System.TimeSpan cannot be converted to an integer. I've tried a million things with variables and it's so complicated in VB.

Like yesterday I had a _time var which was set to DateTime. It wouldn't let me if(_time = 20) or even if (_time).

Any ideas on how to get the subtraction to work?

Thanks!
 
_stopTime.Subtract(_startTime) returns a TimeSpan object. If you want to convert this TimeSpan to a number of seconds then you need to use its TotalSeconds property. Note that TimeSpan.TotalSeconds is of type Double as it returns the fractional part as well. It you want to just get the number of seconds as an integer, here are the three most sensible methods you could use:
VB.NET:
[color=Green]'Round any fractional seconds down to the nearest whole.[/color]
_totalSeconds = Math.Floor(_stopTime.Subtract(_startTime).TotalSeconds)

[color=Green]'Round any fractional seconds up to the nearest whole.[/color]
_totalSeconds = Math.Ceiling(_stopTime.Subtract(_startTime).TotalSeconds)

[color=Green]'Round any fractional seconds up or down to the nearest whole.[/color]
_totalSeconds = Math.Round(_stopTime.Subtract(_startTime).TotalSeconds)
I suggest that you look in the help at the members of the TimeSpan structure.

Edit:
With regards to the issue you mention last, you are trying to compare apples and oranges. A DateTime is a complex structure, so you cannot simply compare it to an integer. It has properties that you can use to return the month as an integer, or the year as an integer, and so on, but the DateTime itself is NOT an integer. You also cannot use just a DateTime as the subject of an If statement. The subject of an If statement must be able to reduced to a boolean value. How can a date be true or false? You can compare one date to another to see if one is before or after the other and things like that. Once again, I suggest you go to the help and check out the help topics entitled "DateTime Structure" and "DateTime Members". In fact, you should always look at the help topics for unfamiliar objects or those that you don't understand fully.
 
to expand a bit on jmcilhinney's last post here there's also

VB.NET:
_totalSeconds = Convert.ToInt32(_stopTime.Subtract(_startTime).TotalSeconds)
 
Back
Top