Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB aplication external process hangs

Status
Not open for further replies.

skynetosiris

Programmer
Jun 2, 2003
15
MX
im using the attached process to run an external program, everything works fine, but whenever the external program hangs my application hangs, how can i implement a timer to stop the application if the external program hangs?

thanks in advance

Public Function ExecuteApp(sCmdline As String) As String
Dim proc As PROCESS_INFORMATION, ret As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim hReadPipe As Long 'Este Handle lee el Pipe.
Dim hWritePipe As Long 'A este pipe se redirecciona StdOutput y StdErr.
Dim sOutput As String
Dim lngBytesRead As Long, sBuffer As String * 256

sa.nLength = Len(sa)
sa.bInheritHandle = True

ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
MsgBox "CreatePipe failed. Error: " & Err.LastDllError
Exit Function
End If

start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
' Redirecciona la salida estandar y los errores al mismo pipe
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe
start.wShowWindow = SW_HIDE

ret = CreateProcessA(0&, sCmdline, sa, sa, True, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret = 0 Then
MsgBox "CreateProcess failed. Error: " & Err.LastDllError
Exit Function
End If

CloseHandle hWritePipe

' Lee el texto del shell
' hasta 256 caracteres a la vez
Do
ret = ReadFile(hReadPipe, sBuffer, 256, lngBytesRead, 0&)
sOutput = sOutput & Left$(sBuffer, lngBytesRead)
Loop While ret <> 0 ' if ret = 0 ya no hay caracteres que leer

CloseHandle proc.hProcess
CloseHandle proc.hThread
CloseHandle hReadPipe

ExecuteApp = sOutput ' Regresa el resultado.
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top