Checking for a Task on windows?

N

NooK

I am having a small problem, I run a program (By running VBA's "Shell
cmd) and this program will create a file so I wait for the fil
creation on excel so I can continue working on it.

Now the problem is that there is no telling how long the file will tak
to be created therefore it makes it hard to set a TimeOut on th
program so that after a certain time if no file found stop execution.

What I wondered is, since the "Shell" cmd returns the Task ID, is ther
any function that I can search if a process is running (By the Tas
ID)? Or something similar? This way as soon as the task is not runnin
anymore I can continue the program and if no file was found after tha
I can stop the program without the user having to wait too long or th
program stopping in the middle of the task execution.

Best Regards

Noo
 
P

papou

Hi Nook
There may be a solution using API functions.
Do a Google search for API Functions.

HTH
Cordially
Pascal
 
N

NooK

Thanks for the help (Nice website btw) but I am a bit confused.
couldn't find any function that will allow me to loop through th
processes and check for their IDs, can you give me a hint or somewher
to start at?

Best Regards

Noo
 
P

papou

Nook
Thanks to TroyW and the valuable info found and Randy Birch's code, here's a
sample code to acheive what you want (simply replace the Command1_Click sub
to you existing macro where the shell command is executed)
HTH
Cordially
Pascal
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2004 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Private
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STATUS_PENDING = &H103&


Private Sub Command1_Click()

RunShell "c:\windows\notepad.exe"

End Sub


Private Sub RunShell(cmdline As String)

Dim hProcess As Long
Dim ProcessId As Long
Dim exitCode As Long

ProcessId = Shell(cmdline, 1)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)

Do

Call GetExitCodeProcess(hProcess, exitCode)
DoEvents

Loop While exitCode = STATUS_PENDING

Call CloseHandle(hProcess)

MsgBox "The shelled process " & cmdline & " has ended."

End Sub
 
N

NooK

Thanks, I've tried but the function gives me the msgbox straight awa
just as the program is starting. Maybe OpenProcess Is returning NULL?
have tried another aproach using FindWindow an
GetWindowThreadProcessId without success.

Any other ideas?

Best Regards

Noo
 
N

NooK

Yes because what happens is that I run a software which will filter
database and return me a dbf file. When I run the software (Process),
small window shows the program running and what it is doing until i
creates the dbf file and closes (The window closes), and I get th
MsgBox right before the software window shows up, so Excel excutio
halts but the software starts.

Any idea?

Best Regards

Noo
 
Top