Retrieve process time in memory in VB

I have certain scrip that gets process amount of memory, however i need not add another case where i need to check also the TIME in memory of the process. I know that when memory is over 40MB it causes trouble, so the process is killed by the script, however i need also to get the time in memory because when its longer than certain minutes it causes trouble as well.

What should i add?

Option Explicit
Dim objWMIService, objProcess, objFSO, objTextFile
dim colProcess,colServices, colItems
dim objItem, objService
dim objFSO2, objTextFile2
Dim strComputer, strProcessKill, strProcessMem
Dim memLimit, processMem
Dim mustKill
Dim WshShell
Const ForAppending = 8

Set WshShell = WScript.CreateObject("WScript.Shell") 
strComputer = "."
strProcessKill = "'DLLHOST.EXE'"
strProcessMem = "'DLLHOST'"
memLimit = 40000
mustKill = 0
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & strProcessKill )



dim intRC
For Each objProcess in colProcess    
    processMem = objProcess.WorkingSetSize/1024
    Set objFSO2 = CreateObject("Scripting.FileSystemObject")
    Set objTextFile2 = objFSO2.OpenTextFile ("c:\ejecuciones_dllhost.txt", ForAppending, True)
    objTextFile2.WriteLine(now & vbnewline & " Process has been CHECKED with " & processMem & " Kb" & vbnewline & vbNewLine)
    objTextFile2.Close

                If processMem >= memLimit Then
		       mustKill = 1
                End If  
  
    if mustKill = 1 then		
 	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile ("c:\informe_dllhost.txt", ForAppending, True)

	objTextFile.WriteLine(now & vbnewline & " Process has been killed with " & processMem & " Kb" & vbnewline & vbNewLine)
	objTextFile.Close
	wshShell.Run "cmd /k C:\pskill.exe -accepteula dllhost.exe", 1, True	
	WScript.Quit
    end if
Next
 
WScript.Quit

Open in new window

dllhost.PNG
LVL 1
celticianAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
You would do well to move the FSO object instantiation (for your logging) outside of the loop.
x-menIT super heroCommented:
There is:
CreationDate :  date process was created (Ex.: 20150715121535.303886+060)


UserModeTime and KernelModeTime : time spent in each mode
David L. HansenCEOCommented:
You may benefit from the ASP profiler, which could help get the time stamps for you. Beyond that, if you work in Visual Studio, the built in profiler is a great tool for doing serious, in-depth, performance analysis.
celticianAuthor Commented:
How would i use a CreationDate instance for this purpose?
x-menIT super heroCommented:
Exaxmple:

Option Explicit
Dim procDateCreate, strComputer, strProcessKill,objWMIService,colProcess,objProcess,TimeToKill,mustKill

strComputer = "."
strProcessKill = "'notepad.EXE'"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & strProcessKill )

For Each objProcess in colProcess    
      
      TimeToKill = DateAdd("n",4,WMIDateStringToDate(objProcess.CreationDate)) 'http://www.w3schools.com/vbscript/func_dateadd.asp
      
      WScript.Echo "proc Name: " & objProcess.name
      WScript.Echo "proc CreationDate: " & objProcess.CreationDate
      WScript.Echo "proc CreationDate (Human): " & WMIDateStringToDate(objProcess.CreationDate)
      WScript.Echo "proc CreationDate + 4minutes: "& TimeToKill

    If DateDiff("s",TimeToKill,WMIDateStringToDate(objProcess.CreationDate))<0 Then      'http://www.w3schools.com/vbscript/func_datediff.asp
       mustKill = 1
        End If  

      WScript.Echo mustKill
      WScript.Echo "proc KernelModeTime (Milliseconds): "& objProcess.KernelModeTime      'Milliseconds
      WScript.Echo "proc UserModeTime (Milliseconds): "& objProcess.UserModeTime      'Milliseconds
Next

Function WMIDateStringToDate(dtmStart)
    WMIDateStringToDate = CDate(Mid(dtmStart, 5, 2) & "/" & _
        Mid(dtmStart, 7, 2) & "/" & Left(dtmStart, 4) _
            & " " & Mid (dtmStart, 9, 2) & ":" & _
                Mid(dtmStart, 11, 2) & ":" & Mid(dtmStart, _
                    13, 2))
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.