Shell still running?

J

Jos Vens

Hi,

I wonder if it's possible to check if an external command is still running,
using the Shell-function.

Here's my code (vCmd = dos-program):

vID = Shell(vCmd, vbMinimizedNoFocus)

is it possible to use vID in order to see if the dos-command is still open?

Thanks,
Jos Vens
 
S

Steve Yandl

Jos,

Here is an example of something you can modify that should work. In my
example, the first message box was used to make sure I gave enough time for
Notepad to actually open before the remaining code checked for the process
ID. In practice, you probably won't need it. I also had Notepad open in a
normal window with focus for purposes of testing but you can easily modify.

'----------------------------------------------
Sub StartProcessThenCheck()
Dim objWMI As Object
Dim colProcesses As Object
Dim objProcess As Object
Dim vID As Variant

vID = Shell("C:\Windows\Notepad.exe", 1)

MsgBox "A new notepad process has been started"

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcesses = objWMI.ExecQuery _
("Select * from Win32_Process")

For Each objProcess In colProcesses
If objProcess.ProcessID = vID Then
MsgBox "The instance of Notepad you just started is still running"
End If
Next objProcess

Set objWMI = Nothing
Set colProcesses = Nothing
Set objProcess = Nothing
End Sub


'---------------------------------------------

Steve Yandl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top