Results 1 to 6 of 6

Thread: Time taken for file execution

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    33

    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

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Time taken for file execution

    VB Code:
    1. Option Explicit
    2.     Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    3. Private Sub Form_Load()
    4.     Dim startTime As Long
    5.     Dim endtime As Long
    6.         startTime = timeGetTime
    7.             'your events
    8.             '...
    9.             '...
    10.             '...
    11.             endtime = timeGetTime
    12.                 MsgBox "Duration time (in milliseconds): " & endtime - startTime
    13. End Sub

  3. #3
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Time taken for file execution

    I doubt you can do it that way. From MSDN -
    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

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Time taken for file execution

    This works very well for me. It works faster with each time (which is quite logical):

    VB Code:
    1. Dim startTime As Long
    2. Dim endtime As Long
    3.     startTime = timeGetTime
    4.     Shell "regedit.exe", 1
    5.     endtime = timeGetTime
    6.         MsgBox "Duration time (in milliseconds): " & endtime - startTime

  5. #5
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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.

    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:-
    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.

  6. #6
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Time taken for file execution

    here's a basic example using EnumWindows and a Timer.
    Last edited by rory; Aug 18th, 2006 at 04:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width