To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old May 15th, 2007, 05:52 PM   #1
phimuskapsi
Junior Member
 
Join Date: Apr 07
Posts: 29
phimuskapsi is an unknown quantity at this point (<10)
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
phimuskapsi is offline   Reply With Quote
Old Aug 15th, 2007, 11:49 PM   #2
anhn
Head Hunted
 
anhn's Avatar
 
Join Date: Aug 07
Location: Australia
Posts: 3,168
anhn is a glorious beacon of light (400+)anhn is a glorious beacon of light (400+)anhn is a glorious beacon of light (400+)anhn is a glorious beacon of light (400+)anhn is a glorious beacon of light (400+)anhn is a glorious beacon of light (400+)
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
anhn is offline   Reply With Quote
Old Aug 16th, 2007, 09:36 AM   #3
phimuskapsi
Junior Member
 
Join Date: Apr 07
Posts: 29
phimuskapsi is an unknown quantity at this point (<10)
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
phimuskapsi is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 02:52 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.