Friday, April 15, 2016

C# VBA Side by Side Time a long running process

Ever have a need to time a long running process?  Well in Excel VBA, its a 2 step process that includes making a declaration to a WinAPI function called GetTickCount() and then directly calling this function in code.  In C#, its a lot simpler because there is  a TickCount property in the Environment class.

The algorithm to time an application is to store the results of System time before starting a long process (start time) and then again store the system timer when the process is done (ending time).

Access VBA

 The elapsed time is the difference between the ending time and start time.  The high level view is to:

Step A: Declare the API call in a separate module. For more detail refer to this article

Step B: Calling the GetTickCount() API function as strategic points in the long running process

Step C: Calculate the elapsed time and update the user


DoCmd.Hourglass True
lngStartTime = GetTickCount()
Call SysCmd(acSysCmdInitMeter, "Downloading files ...", Me.lvHistory.ListItems.Count)
For Each objli In Me.lvHistory.ListItems
        Call CreateDailyWorkWB5(objli.SubItems(DailyWorkDetail.ClientID), _
             objli.SubItems(DailyWorkDetail.ClientName), _
             COB_Date, _
             intVersionNo, _
             intPriorVersionNo:=getPriorVersionNo())
               
         iCounter = iCounter + 1
         Call SysCmd(acSysCmdUpdateMeter, iCounter)
                
           
Next
lngEndTime = GetTickCount()

lngElapsedTime = (lngEndTime - lngStartTime) * 0.001
MsgBox "Just exported " & Format(iCounter, "##,##0") & " record(s) in " & _
     Format(lngElapsedTime, "##,##0") & " second(s) ", vbInformation, "Results"

C#


intStartTime = Environment.TickCount;
recordsAffected=objTask.RefreshCDLReferenceFile(strFileName,strFileNameAccess:strAccessDb);
intEndTime = Environment.TickCount;
decElapsedTime = (decimal)((intEndTime - intStartTime) * .001);
MessageBox.Show("Imported " + recordsAffected.ToString() + " row(s) in " + decElapsedTime.ToString() + " sec(s)", "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);

See Also


More info about the Tickcount property