Sunday, May 15, 2016

Process Executor to execute external process

Today I found a Nuget package name Process Executor in. I often make program that need to run executables. So I decided to take a look on this but I don't have the time for now. so I bookmarked this.

Some quote from the site:
DescriptionProcessExecutor is a wrapper class for System.Diagnostic.Process which created with intention to manage external process within .NET application without showing up the console window and capture all the console output logs at the same time. Download From...GitHubNuGetCodeProjectClass Diag

Description

ProcessExecutor is a wrapper class for System.Diagnostic.Process which created with intention to manage external process within .NET application without showing up the console window and capture all the console output logs at the same time.



Download From...




Class Diagram

Process Executor - Class Diagram


Features

  • Execute external applications.
  • Configurable working directory.
  • Accept process arguments.
  • Abort created process and it's child processes.
  • Thread safe, support multi-threaded execution.
  • Show / hide console window during process execution.
  • Enable / disable console output to System.Diagnostics.Trace.
  • Execute process as different user.
  • Capture standard output and standard error from console application.
  • Capture output to log file.
  • Subclass as tool-specific executor.
  • Option to wait for process complete. 
  • Notification when process completed.

    Usage Guide

    • Assign Application, Arguments and WorkingDirectory.
    • Call Execute() to start.
      • Process Executor will start process by calling Process.Start internally.
      • Process Executor will subscribe to OutputDataReceived and ErrorDataReceived events from ProcessHandler to capture all console output.
      • For each captured output and error messages:
        • If Output file is defined, write message to output file.
        • If option TraceLogEnabled is set, write message to System.Diagnostics.Trace.
      • Wait for process to complete if waitForExit flag is set, else return immediately.
      • At the end of execution, return error code and console output messages as ProcessResult object.
    • To terminate an executing process, call Abort() to stop process and all child processes. See section Abort for more details.

    Capture Output Data

    The two functions below shows how output from console window is captured in Trace and output log.
    When use together Diagnostics TextBox with option ShowConsoleWindow disabled and TraceLogEnabled enabled, you will have all the console output from the triggered process redirect to your application. This is even better when the ProcessExecutor is started in worker thread.

            <span style="color: blue;">private</span>

    Abort

    The Abort function in ProcessExecutor is useful to terminate the spawned process either when error is detected or decided to terminate by user. The Abort function will terminate the process and all its child process. This is handy to terminate all the spawned process when application in terminated.

    The terminate function was not part of System.Diagnostics.Process. We make use of ManagementObjectSearcher to search for object with given object ID obtained from ProcessHandler when process started. All child process created by its parents will also be terminated. This is useful when you want to terminate a batch file and all the processes spawned from the batch file.

            <span style="color: blue;">private</span>

    Subclass

    ProcessExecutor can be use as base class for dedicated tool such as Windows Installer (msiexec.exe) and Visual Studio (devenv.com) which provide more than just process execution.

    Example: WindowsInstallerClient with Install and Uninstall function.

        <span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><summary></span>
        <span style="color: grey;">///</span><span style="color: green;"> Windows Installer Client (msiexec.exe).</span>
        <span style="color: grey;">///</span><span style="color: green;"> Uninstallation require the original MSI package.</span>
        <span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"></summary></span>
        <span style="color: blue;">public</span>

    No comments:

    Post a Comment