Recording correct time for routine to run

vb.net 2003 sp4
I have a routine that takes a while to run sometimes.
its posts the time on a form as it runs.

It seems as if if the screen saver kicks in its throws off the time..
or could just be bad code..?


I cannot make



public sub Routine()
Dim startDT As DateTime = DateTime.Now
....
' update form
        Me.Label17.Text = startDT
code


' finish it up
        Dim endDT As DateTime = DateTime.Now
        Dim elapsed As TimeSpan = endDT.Subtract(startDT)
        Dim strEnd As String
        strEnd = "Project Completed In: "
        Label17.Text = strEnd & elapsed.Minutes & " mins " & elapsed.Seconds & " secs"
        Me.Label17.Refresh()

end sub


Thanks
fordraiders
LVL 3
FordraidersAsked:
Who is Participating?
 
Bob LearnedCommented:
Better example:


        Dim watch As New Stopwatch()
        watch.Start()
 
        ' Do something
 
        watch.Stop()
 
        Dim totalSeconds As Double = watch.Elapsed.TotalSeconds

Open in new window

0
 
DabasCommented:
What exactly do you mean by "It seems as if if the screen saver kicks in its throws off the time.."

Code looks OK. What exactly the problem is, is not clear.

Dabas
0
 
FordraidersAuthor Commented:
I know the programs runs at least 1-1/2 but it says at the end of the orutine.. 8min 30secs?

The screen saver is just a wild guess as to what the problem is..
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
DabasCommented:
I have tested on my system, and it seems to work OK.

Try one or more of the following: Turning your screen saver off
Running the routine on another computer
Check your code that you are not changing the value of startDT anywhere
0
 
Bob LearnedCommented:
I would think that you would need something like a Stopwatch:

Dim watch As New Stopwatch()
watch.Start()

...

watch.Stop()
0
 
FordraidersAuthor Commented:
do I set a variable to read the start and stop of the watch ?

dim stSartWatch as datetime ?

0
 
FordraidersAuthor Commented:
Cao, Do i need to decalre any special "Imports"
0
 
FordraidersAuthor Commented:
Cap , Something I just thought of... If the timer goes beyond 1 hour...does it reset and still just give me the minutes past one hour duration ?


so if I ran... 1hour 5 min 25 sec

I'm only telling it to bring me back min and secs ?


0
 
Bob LearnedCommented:
The Stopwatch is in the System.Diagnostics namespace.  The timer covers the same time period as a TimeSpan, since that what the Elapsed property is.  It will not reset after an hour.  

1 hour 5 min 25 sec = 3925 seconds.

Since Elapsed is a TimeSpan instance, there are other properties associated with it, like Hours, Minutes, Seconds, etc.
0
 
FordraidersAuthor Commented:
Working good now...Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.