Example #1
0
        public static ProcessThread ExecuteProcessInSeparateThread(ProcessStartInfo processStartInfo, int maxStackSizeInMB, 
            ProcessWriteLineRedirectDelegate processWriteLineRedirectDelegate, ProcessThreadCompleteHandler completeHandler)
        {
            var processThread = new ProcessThread(processStartInfo, completeHandler, processWriteLineRedirectDelegate);

            Thread thread = null;
            if (maxStackSizeInMB > 0)
                thread = new Thread(new ThreadStart(processThread.Start), maxStackSizeInMB * 1024 * 1024);
            else
                thread = new Thread(new ThreadStart(processThread.Start));

            thread.Start();

            return processThread;
        }
 public ProcessThread(ProcessStartInfo startInfo, ProcessThreadCompleteHandler completeHandler, ProcessWriteLineRedirectDelegate processWriteLineRedirectDelegate)
     : this(startInfo, completeHandler, 0, processWriteLineRedirectDelegate)
 {
 }
 public ProcessThread(ProcessStartInfo startInfo, ProcessThreadCompleteHandler completeHandler, int secondsToTimeout, ProcessWriteLineRedirectDelegate processWriteLineRedirectDelegate)
     : this(startInfo, completeHandler, secondsToTimeout)
 {
     this.WriteLineRedirect = processWriteLineRedirectDelegate;
 }
 public ProcessThread(ProcessStartInfo startInfo, ProcessThreadCompleteHandler completeHandler, int secondsToTimeout)
     : this(startInfo, completeHandler)
 {
     this.TimeoutInSeconds = secondsToTimeout;
 }
 public ProcessThread(ProcessStartInfo startInfo, ProcessThreadCompleteHandler completeHandler)
 {
     this.StartInfo        = startInfo;
     this.CompleteHandler  = completeHandler;
     this.TimeoutInSeconds = 0;
 }
        private static int CreateProcess(string fileName, string commandLineArgs, string workingFolder, ProcessThreadCompleteHandler completeHandler, int secondsToTimeout, bool windowVisible)
        {
            // Create the process.
            var processStartInfo = new ProcessStartInfo();

            processStartInfo.FileName         = fileName;
            processStartInfo.Arguments        = commandLineArgs;
            processStartInfo.WorkingDirectory = workingFolder;

            // Set window visibility.
            if (windowVisible)
            {
                processStartInfo.CreateNoWindow = false;
                processStartInfo.WindowStyle    = ProcessWindowStyle.Normal;
            }
            else
            {
                processStartInfo.CreateNoWindow = true;
                processStartInfo.WindowStyle    = ProcessWindowStyle.Hidden;
            }

            // If running the complete handler, start the process as a new thread.
            if (completeHandler != null)
            {
                ExecuteProcessInSeparateThread(processStartInfo, completeHandler);
                return(0);
            }
            // Else create and start the process.
            else
            {
                var process = new Process();
                process.StartInfo           = processStartInfo;
                process.EnableRaisingEvents = false;
                process.Start();

                if (secondsToTimeout == 0)
                {
                    process.WaitForExit();
                }
                else
                {
                    process.WaitForExit(1000 * secondsToTimeout);
                    if (!process.HasExited)
                    {
                        process.Kill();
                    }
                }

                return(process.ExitCode);
            }
        }
 public static void ExecuteProcessInSeparateThread(ProcessStartInfo processStartInfo,
                                                   ProcessWriteLineRedirectDelegate processWriteLineRedirectDelegate, ProcessThreadCompleteHandler completeHandler)
 {
     ExecuteProcessInSeparateThread(processStartInfo, 0, processWriteLineRedirectDelegate, completeHandler);
 }
 public static void ExecuteProcessInSeparateThread(ProcessStartInfo processStartInfo, ProcessThreadCompleteHandler completeHandler)
 {
     ExecuteProcessInSeparateThread(processStartInfo, 0, null, completeHandler);
 }
 public static void ExecuteProcessInSeparateThread(string fileName, string commandLineArgs, ProcessThreadCompleteHandler completeHandler)
 {
     ExecuteProcessInSeparateThread(new ProcessStartInfo(fileName, commandLineArgs), completeHandler);
 }
 public static int ExecuteProcess(string fileName, string commandLineArgs, string workingFolder, ProcessThreadCompleteHandler completeHandler, bool windowVisible)
 {
     return(CreateProcess(fileName, commandLineArgs, workingFolder, completeHandler, 0, windowVisible));
 }