Spawn Command-line Processes Component


With this component you can spawn command-line processes and obtain the output from them while they are running. The spawned process is invisible to the desktop and the host application can terminate the process at any time. There are also methods to redirect stdin and stderr for the spawned process. Using these you can create a processing chain by redirecting the output from one instance to the input of another.

To use the component set-up the ProcessName parameter to the full path and filename of the process to spawn. Then call the CreateProcess method. This starts the process. To terminate the process before it has finished call the DestroyProcess method. To start the process in a suspended state, set the Suspended property to true, then use the ResumeProcess method to continue running it.

The output of the process is captured in a buffer which is periodically sent to the host application via the OnProcessText event. The buffer size can be changed by setting the BufferSize property. Setting this property higher can improve the execution time of the spawned process. The ProcessText event must be handled quickly otherwise this can also reduce the efficiency of the spawned process. It is therefore advisable not to call VCL functions from this event. Instead capture and process the text returned, then either on a separate thread or on a timer, update the UI with the results.

The OnProcessText event is called from a thread. To improve efficiency this call is NOT synchronised. To help you make your code thread-safe the component contains two methods, EnterCritical and LeaveCritical. These methods use a CrticalSection built into the component so that you can serialise your access to objects and memory.

Source Code

As promised you can download the Delphi and C++ Builder source code in its entirety

Delphi Central - Delphi Programming Tutorials, Hints and Tips