Time taken for file execution
Hi all,
When I used the following code to get the time taken for the batch file to execute, it returned 0 milliseconds. I tried using TimeGetTime also, bt even taht returned different answers, sometime 0 ms. Can anyone tell me whats wrong with the foll. code?
Private sub Build_Click()
ChDrive App.Path
ChDir App.Path
Dim retval As Long
retval = GetTickCount()
DoEvents
Call Shell("BuildALL.bat", vbNormalFocus)
MsgBox (GetTickCount - retval) & " miliseconds elapsed"
End Sub
Re: Time taken for file execution
VB Code:
Option Explicit
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Sub Form_Load()
Dim startTime As Long
Dim endtime As Long
startTime = timeGetTime
'your events
'...
'...
'...
endtime = timeGetTime
MsgBox "Duration time (in milliseconds): " & endtime - startTime
End Sub
Re: Time taken for file execution
I doubt you can do it that way. From MSDN -
Quote:
By default, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed
Re: Time taken for file execution
This works very well for me. It works faster with each time (which is quite logical):
VB Code:
Dim startTime As Long
Dim endtime As Long
startTime = timeGetTime
Shell "regedit.exe", 1
endtime = timeGetTime
MsgBox "Duration time (in milliseconds): " & endtime - startTime
Re: Time taken for file execution
@ gavio. Does it give the time that regedit is actually open ? ie until you close it yourself ? Or does it just return the time taken for windows to launch regedit ? Not sure at this point if rengask wants the shelled programs actual running time. :confused:
Just in case.... There have been quite a few queries recently on waiting for shelled processes to finish before executing other code. Do a search and you'll get lots of hits.
More from MSDN:-
Quote:
You must use several other Windows API functions to launch and wait for the program to be terminated. The ExecuteAndWait method, shown below, can address this problem quite nicely. All you have to do is provide the full pathname and its required arguments to this method. The application you have just launched will retain the focus until it is subsequently terminated.
Public Sub ExecuteAndWait(cmdline As String)
Dim NameOfProc As PROCESS_INFORMATION
Dim NameStart As STARTUPINFO
Dim X As Long
NameStart.cb = Len(NameStart)
X = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
X = CloseHandle(NameOfProc.hProcess)
End Sub
As you can see from the code above, the ExecuteAndWait method uses several functions—CreateProcessA, WaitForSingleObject, and CloseHandle. We have already seen that a Windows application program can be executed by calling the ShellExecute function. You can also use the CreateProcessA function to launch applications and to force that application to retain the focus until it is terminated.
Re: Time taken for file execution
here's a basic example using EnumWindows and a Timer.