WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Process ID

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Close All Instances of an Application

 Keywords: Close All Instances of an Application 

Question:

I need to close all instances of a particular application that are using greater than 5 percent of the processor and have been running for more than 5 minutes. How can this be accomplished?

Answer:

Using the Process Extender.
appname = "notepad.exe"
;Addextender("wproc34i.dll")
Addextender("wwprc44i.dll")

;Grab a list of all processes
proclist  = tListProc()
proclist  = StrLower(proclist)
proccount = ItemCount(proclist,@TAB)
list = ""

;Make a list of processs ids of each instance of the app
For xx = 1 To proccount
	thisitem   = ItemExtract(xx,proclist,@TAB)
	thismodule = ItemExtract(1,thisitem,"|")
	thispid    = ItemExtract(2,thisitem,"|")
		If thismodule == appname
		If list =="" 
			list = thispid
		Else
			list = StrCat(list, @TAB, thispid)
		Endif
	EndIf
Next

;Grab data for each instance
count = ItemCount(list, @TAB)
for xx = 1 to count
	procid = ItemExtract(xx, list, @TAB)
	proctimestr = StrCat("\Process(",FileRoot(appname),"#",xx-1,")\%% Processor Time")
	elapsedtimestr = StrCat("\Process(",FileRoot(appname),"#",xx-1,")\Elapsed Time" )
	procidstr = StrCat("\Process(",FileRoot(appname),"#",xx-1,")\ID Process")
	percentproctime = tGetData(proctimestr, 0)
	elapsedtime = tGetData(elapsedtimestr,0)
	procid = tGetData(procidstr,0)

	text = strcat("%%CPU=", percentproctime, @crlf,"Elapsed=", elapsedtime, @CRLF)
	Message("Process ID %procid%",  text)

	If percentproctime >   5     ; greater than 5 percent
		If elapsedtime  > 300     ; greater than 300 seconds or 5 minutes
			hProcess=tOpenProc(procid,3);PROCESS_TERMINATE
			If hProcess ;if valid handle
				tKillProc(hProcess)
				tCloseProc(hProcess)  
				Message("tKillProc","Process %procid% is terminated")
			Endif
		EndIf
	EndIf
Next


Article ID:   W15858
File Created: 2013:04:01:09:08:36
Last Updated: 2013:04:01:09:08:36