Solved

suspend flow until a child process completes

Posted on 2004-03-29
7
151 Views
Last Modified: 2010-05-02
here's the psuedo code:

from inside a running exe:

for each row in dataset
   create a new process (from a console exe) with parameters based on datarow
next

currently what is happening is that i get multiple instances of the console.exe but what I want is for one instance to run (takes about 10 minutes) then go and process the next row of the dataset.

does anyone know how to wait for the new process to complete before continuing to the next line of code?

Thank you,

Jon
0
Comment
Question by:jonleehacker
7 Comments
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
How are you starting the New process?
Is this .NET or VB6?
0
 
LVL 4

Expert Comment

by:sokolovsky
Comment Utility
Look at this code:

Const INFINITE = &HFFFF
Const STARTF_USESHOWWINDOW = &H1
Private Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
End Enum
Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type
Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type
Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type
Private Enum enPriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
End Enum
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Function RunAndWait(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
    Dim pclass As Long
    Dim sinfo As STARTUPINFO
    Dim pinfo As PROCESS_INFORMATION
    'Not used, but needed
    Dim sec1 As SECURITY_ATTRIBUTES
    Dim sec2 As SECURITY_ATTRIBUTES
    'Set the structure size
    sec1.nLength = Len(sec1)
    sec2.nLength = Len(sec2)
    sinfo.cb = Len(sinfo)
    'Set the flags
    sinfo.dwFlags = STARTF_USESHOWWINDOW
    'Set the window's startup position
    sinfo.wShowWindow = start_size
    'Set the priority class
    pclass = Priority_Class
    'Start the program
    If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
    0&, WorkDir, sinfo, pinfo) Then
        'Wait
        WaitForSingleObject pinfo.hProcess, dwMilliseconds
        RunAndWait = True
    Else
        RunAndWait = False
    End If
End Function
Private Sub Form_Load()
    Call RunAndWait("cmd.exe", "C:\", INFINITE, SW_NORMAL, HIGH_PRIORITY_CLASS)
    MsgBox ("Child Application is closed")
End Sub
0
 
LVL 1

Author Comment

by:jonleehacker
Comment Utility
Hi Everyone,

The program is in .Net (thank god! by the look of the code that sokolovsky posted).

I have discovered the answer on my own and it is very simple.

            Dim newProcess As New System.Diagnostics.Process
            newProcess.StartInfo.FileName = "notepad.exe"
            newProcess.StartInfo.UseShellExecute = False
            newProcess.StartInfo.Arguments = "text.txt"
            newProcess.StartInfo.RedirectStandardOutput = False
            'Don't show a command prompt window
            newProcess.StartInfo.CreateNoWindow = True
            newProcess.Start()
            newProcess.WaitForExit()       <--*****************This does the trick
            newProcess.Dispose()

Take care,

Jon
0
 
LVL 4

Expert Comment

by:sokolovsky
Comment Utility
>The program is in .Net (thank god! by the look of the code that sokolovsky posted).
:)
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
Comment Utility
User resolved; closed, 125 points refunded.

Netminder
Site Admin
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now