|
|
#1 |
|
Junior Member
Join Date: Apr 07
Posts: 29
![]() |
Shell And Wait Question (advanced)
I have modified a shell and wait script that checks to see if a NET USE statement (run in a batch file) has taken longer than 7 seconds. However I can't think of a good way to do it. I tried the timer and that doesn't work the way I'm doing it.
If anyone has a recommendation on a way of doing it let me know. Code:
Private Function shellAndWait(ByVal fileName As String) As Long
Dim executionStatus As Long
Dim processHandle As Long
Timer1.Interval = 1000
Timer1.Enabled = True
timercnt = 1
'Execute the application/file, but HIDE the window
executionStatus = Shell(fileName, vbNormalFocus)
'Get the Process Handle
processHandle = OpenProcess(&H100000, True, executionStatus)
'Wait till the application is finished
Do
returnValue = WaitForSingleObject(processHandle, 50)
DoEvents
If bCancel Then
SendMessage FindWindow(WinCmdPath, vbNullString), WM_CLOSE, 0&, 0&
returnValue = -1
Exit Do
End If
Loop Until returnValue = 0
If returnValue = 0 Then
Timer1.Enabled = False
timercnt = 1
End If
bCancel = False
shellAndWait = returnValue
End Function
Private Sub Timer1_Timer()
If timercnt = 7 Then
bCancel = True
timercnt = 1
Else
timercnt = timercnt + 1
End If
End Sub
|
|
|
|
|
|
#2 |
|
Head Hunted
Join Date: Aug 07
Location: Australia
Posts: 3,168
![]() ![]() ![]() ![]() ![]() ![]() |
Re: Shell And Wait Question (advanced)
Why don't you use this instead of a timer. it's much more simple:
Code:
t = Timer
Do
ret = WaitForSingleObject(processHandle, 50)
DoEvents
Loop Until (ret = 0) Or (Timer - t > 7)
If ret <> 0 Then
SendMessage FindWindow(WinCmdPath, vbNullString), WM_CLOSE, 0&, 0&
ShellAndWait = -1
Else
ShellAndWait = 0
End If
|
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Apr 07
Posts: 29
![]() |
Re: Shell And Wait Question (advanced)
I actually ended up with this:
Code:
Private Function shellAndWait(sShellFile As String) As Long
sFile = sShellFile
lProcessID = Shell(sFile, vbHide)
If lProcessID <> 0 Then
lHandle = OpenProcess(SYNCHRONIZE, 0, lProcessID)
If lHandle <> 0 Then
lReturn = WaitForSingleObject(lHandle, 5000)
If lReturn > 0 Then
ErrorCode = "30"
ErrorStatus = 1
Call ErrorHandling(rMachine, lUsername, i, ErrorCode)
' Get the target's window handle.
target_hwnd = FindWindow(vbNullString, WinCmdPath)
If target_hwnd = 0 Then
Exit Function
End If
' Send the application the WM_CLOSE message.
PostMessage target_hwnd, WM_CLOSE, 0, 0
CloseHandle (lHandle)
End If
End If
End If
shellAndWait = returnValue
End Function
|
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|