Testing for running process????

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Testing for running process????

Post by kriemer » Sat Oct 31, 2009 3:01 am

I am a very new newbie to Macro Scheduler, so bear with my initial flurry of questions, please.

I currently use the following code to test if Excel is running and if it is not, a subsequent script runs as "next step" of my scheduling script. Similar code is applied to testing for other running process types.

Code: Select all

Sub CheckProcessStatusExcel
Const PROCESS_NAME = "EXCEL.EXE"  
Dim objWMI : Set objWMI = GetObject("winmgmts:\\.\root\cimv2")  
Dim colProcessList : Set colProcessList = objWMI.ExecQuery("Select * from Win32_Process Where Name = '" & PROCESS_NAME & "'")
Do While colProcessList.Count <> 0
WScript.Sleep(5000)  
Set colProcessList = objWMI.ExecQuery("Select * from Win32_Process Where Name = '" & PROCESS_NAME & "'")
Loop
WScript.Sleep(1000)
End Sub
I suspect that a looping decision block could be structured to accomplish the same thing; and could have the additional advantage of being a sort of master sub that could be easily inserted multiple times in a script.

Problem is that I don't see any Macro Script code that will look for running processes.

Is there any?


Thanks

k

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Sat Oct 31, 2009 4:30 am


kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Sat Oct 31, 2009 3:21 pm

So I see that there is no native Macro Scheduler code to test for running processes. I also see that my VBS code is very similar to the code I was directed to.

What I was trying to figure out is how to build code so that if:
- Excel is running (1) do this;
- Excel is not running (0) do something else

As an additional constraint, I would like to use Macro designer; as my code often has to be shown to a number of people and the flow chart representation would facilitate this.

Thanks 10^6

k

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Oct 31, 2009 9:56 pm

You could do:

Code: Select all

IfWindowOpen>Microsoft Excel*
  //do this
Else
  //do something else
Endif
Or:

Code: Select all

VBSTART
'returns the number of copies of ProcessName that are running
'will return 0 if ProcessName is not running
Function IsProcessRunning(ProcessName)
	Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where Name='" & ProcessName & "'")
	IsProcessRunning = colProcessList.count
End Function
VBEND

VBEval>IsProcessRunning("excel.exe"),res
If>res>0
  //is running 
Else
  //is not running - do something else
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sat Oct 31, 2009 11:06 pm

No time for full code, but using DOS commands should also work:

Code: Select all

IfFileExists>temp.txt then del temp.txt
RunProgram>cmd /c TASKLIST /FI "IMAGENAME eq EXCEL.EXE" | FIND "excel.exe">temp.txt
IfFileExists>temp.txt is true then excel is running and do something.
IfFileExists>temp.txt is false then excel is not running and do something else.
IfFileExists>temp.txt then del temp.txt
Sample code above is untested outline and the syntax is not correct. I am on the run, gotta go. Good luck.....
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Sun Nov 01, 2009 4:51 pm

Got it.

Works a treat. Many thanks for the code.

k

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts