Elapsed time count

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
How to use timer to count an elapsed time? For example I want to calculate the elapsed time of the execution of application until it returns a solution. The timer will count until the the output is displayed in the textbox.

if textbox1.text<>"" then stop the timer and return the count time in the form of, for example like 0.42321, because the execution time might be less time one second. What is the time interval that I need to set?
 
There is no need for a Timer in this situation. Timers are designed to make something happen ata predetermined time. To measure elapsed time you simply save the current time when you start, save the current time when you finish and then subtract to get the difference:
VB.NET:
Dim start As Date = Date.Now

'Do something here.

Dim end As Date = Date.Now
Dim duration As TimeSpan = end.Subtract(start)
You then use the properties of the TimeSpan object as you need. For instance, it has TotalSeconds and TotalMilliseconds properties, which are likely to be the ones you're interested in. Make sure you read the relevant help topics so that you understand the difference between the properties that start with "Total" and those that don't, e.g. Seconds and TotalSeconds.
 
You can code whatever you want ... i.e. you can populate listview control
VB.NET:
[LEFT]Dim start As Date = Date.Now

For i as integer = 0 to 10000
   me.Listview1.Items.add("item: " & i)
Next

Dim end As Date = Date.Now
Dim totalTime As TimeSpan = end.Subtract(start)
MessageBox.Show(totalTime.Duration.ToString)

After this code you will know how much time is needed to populated listview control with 10.000 items ;)
[/LEFT]
 
retkehing said:
What do I have to code in between Dim start As Date and Dim end As Date ?
The whole point here was to time something, so do whatever you want to time in there.
 
Back
Top