Gather the runtime of a process

  • Thread starter Federico G. Babelis
  • Start date


Federico G. Babelis

Hi All:

I need to gather the runtime in format "HH:mm:ss" of certain processes from
its start to its end, any ideas ?




Jay B. Harlow [MVP - Outlook]

I normally use QueryPerformanceCounter as its in the units used by
Performance Counters:

Something like:

Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef counter
As Long) As Boolean
Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
frequency As Long) As Boolean

' set some time var
Dim start, finish, frequency As Long

' work

Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start) /

Alternatively you can use DateTime:

' set some time var
Dim start, finish As DateTime
start = DateTime.Now

' work

finish = DateTime.Now
Dim time As TimeSpan = finish.Subtract(start)

A third alternative would be to use "Ticks"

' set some time var
Dim start, finish As Integer
start = Environment.TickCount

' work

' set second time var and comapre to get result
finish = Environment.TickCount
Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish - start)

My understanding is that QueryPerformanceCounter will normally be a higher
resolution then Environment.TickCount, however QueryPerformanceCounter may
not be available.

VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the choice by
providing a System.Diagnostics.Stopwatch class that will automatically
choose between QueryPerformanceCounter & Environment.TickCount...

Hope this helps

Federico G. Babelis

Impressive !

Thanks for your help !

PS: How can i transform the TimeSpan results into format HH:mm:ss ?


Larry Lard

Federico said:
PS: How can i transform the TimeSpan results into format HH:mm:ss ?
You are lucky in that "hh:mm:ss" is the default string conversion of a
TimeSpan, so just MyTimeSpan.ToString() will give you what you want.



Jay B. Harlow [MVP - Outlook]

As Larry suggests, calling TimeSpan.ToString() will return the results in
the format: [-][d.]hh:mm:ss[.ff]

Remember the ToString method of "formattable" objects in .NET is normally
overridden to provide formatting for that object.

If you don't want the fractional seconds or days on the formatted string, I
normally convert the TimeSpan to a DateTime & then use custom DateTime

Note TimeSpan itself only supports a fixed format, I will convert a TimeSpan
into a DateTime if I need custom formatting.

Dim ts As TimeSpan
Dim dt As DateTime = DateTime.MinValue.Add(ts)
Dim s As String

s = ts.ToString() ' default TimeSpan formatting
s = dt.ToString("H:mm:ss") ' custom DateTime formatting

For details on custom datetime formats see:

For information on formatting in .NET in general see:

Hope this helps

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Create control at runtime with process 0
Processes 0
process 1
starting a process and monitoring that process 2
Get Child Process of a Parent Process 0
Events at runtime... 3
Runtime Memory 2
Readonly at runtime 4