Results 1 to 5 of 5

Thread: Shell command to check if program stops running!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    singapore
    Posts
    86

    Unhappy Shell command to check if program stops running!

    I'm doing a VBA program whereby it reads text files(That is not the problem). Those text files are created by a perl program. At one point, I would need to click a button in my Userform to activate the perl program to execute. Also, to prevent the compiler from reading the next line in my code(y? coz the text files wouldn't be created yet coz they are BIG n SEVERAL of them need to be created), how am I to pause my program to check whether the files are created(NOT IF THE FILES R THERE COZ THE FILES MAY EXISTS BUT NOT ALL THE DATA ARE CREATED.HOPE U UNDERSATND THIS) OR whether the perl program has stopped running.I've got a "plain" Shell func but not with this conditions to loop n check. Any body who knows this pls help .Attached is how my Shell func is like(ive put it in a notepad).Feel free to make changes in it and send me if u know how to work it out.Thanks a 1000 times.
    Attached Files Attached Files

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Galway, Ireland
    Posts
    316
    Hello Singapore,
    Bet its warmer there.

    Anyway to the point.

    I presume that your perl program opens the text file for write thus locking it.

    what you could do is a loop with a sleep in it (trapping the error) and keep retrying until you can open the textfile exclusively for reading!!

    On Error Resume Next
    Dim finished As Boolean
    finished = False

    Do While finished = False
    Open "c:\john.txt" For Binary Access Read Lock Read As #1
    finished = True
    Loop
    Close #1
    Slan

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Here is a function that starts a process, and wits until it has finished, or the timeout is passed.
    VB Code:
    1. Private Type STARTUPINFO
    2.     cb As Long
    3.     lpReserved As String
    4.     lpDesktop As String
    5.     lpTitle As String
    6.     dwX As Long
    7.     dwY As Long
    8.     dwXSize As Long
    9.     dwYSize As Long
    10.     dwXCountChars As Long
    11.     dwYCountChars As Long
    12.     dwFillAttribute As Long
    13.     dwFlags As Long
    14.     wShowWindow As Integer
    15.     cbReserved2 As Integer
    16.     lpReserved2 As Long
    17.     hStdInput As Long
    18.     hStdOutput As Long
    19.     hStdError As Long
    20. End Type
    21.  
    22. Private Type PROCESS_INFORMATION
    23.     hProcess As Long
    24.     hThread As Long
    25.     dwProcessId As Long
    26.     dwThreadID As Long
    27. End Type
    28.  
    29. Const NORMAL_PRIORITY_CLASS = &H20
    30. Const IDLE_PRIORITY_CLASS = &H40
    31. Const HIGH_PRIORITY_CLASS = &H80
    32.  
    33. Private Declare Function WaitForSingleObject Lib _
    34.     "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds _
    35.     As Long) As Long
    36.  
    37. Private Declare Function CreateProcessA Lib "kernel32" _
    38.     (ByVal lpApplicationName As Long, ByVal lpCommandLine As _
    39.     String, ByVal lpProcessAttributes As Long, ByVal _
    40.     lpThreadAttributes As Long, ByVal bInheritHandles As Long, _
    41.     ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
    42.     ByVal lpCurrentDirectory As Long, lpStartupInfo As _
    43.     STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) _
    44.     As Long
    45.  
    46. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) _
    47.     As Long
    48.  
    49. Public Sub ShellNWait(CommandLine As String, Optional WaitForMilliseconds As Long = -1&, Optional Priority As Long = NORMAL_PRIORITY_CLASS)
    50.     Dim proc As PROCESS_INFORMATION
    51.     Dim Start As STARTUPINFO
    52.     Dim lRet As Long
    53. On Error Resume Next
    54.  
    55.     'Initialize the STARTUPINFO structure:
    56.     Start.cb = Len(Start)
    57.    
    58.     'Start the shelled application:
    59.     lRet = CreateProcessA(0&, CommandLine, 0&, 0&, 1&, _
    60.     Priority, 0&, 0&, Start, proc)
    61.    
    62.     'Wait for the shelled application to finish:
    63.     'use -1 to specify no "timeout" length.
    64.     lRet = WaitForSingleObject(proc.hProcess, WaitForMilliseconds)
    65.     lRet = CloseHandle(proc.hProcess)
    66.     lRet = CloseHandle(proc.hThread)
    67. End Sub

    P.S. I copied the code from a post brenaaro posted, and modified it a little to fix a little bug.
    Here is the original thread:
    http://www.vbforums.com/showthread.p...hreadid=221401
    Frans

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    singapore
    Posts
    86
    Frans C...Did u look into my attached file.I've got a "similar" code there but I dont c any condition there to check my files ....?! Is there?? And for u , John, where should I place the code..according to tat shell functionwhich is attached?? Thanks for both ur replies..Hope to hear from u.

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    You could check the files last modified date, using VB's FileDateTime function
    Frans

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width