Exemple #1
0
        /// <summary>
        /// Kill managed process, if there is running one
        /// </summary>
        /// <returns></returns>
        public virtual bool Kill()
        {
            if (IsRunning)
            {
                try
                {
                    CurrentCommandLine = "";
                    SystemProcessObject.Kill();
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    SystemProcessObject.Dispose();
                    SystemProcessObject = null;
                }
            }

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Launch programm in separate process
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="workingDir"></param>
        /// <param name="msecLockTimeout">if == 0, then parent process does not lock</param>
        /// <returns></returns>
        private CmdResult RunProcess(
            string parameters   = "",
            string workingDir   = null,
            int msecLockTimeout = 0)
        {
            CmdResult result = null;

            if (SystemProcessObject != null)
            {
                throw new ProcessAlreadyStartedException();
            }

            var process = new Process();

            try
            {
                var startInfo = new ProcessStartInfo
                {
                    WindowStyle            = ProcessWindowStyle.Hidden,
                    FileName               = QuoteString(AppPath),
                    Arguments              = parameters,
                    UseShellExecute        = false,
                    RedirectStandardError  = true,
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    CreateNoWindow         = true,
                    StandardOutputEncoding = Encoding.GetEncoding(866),
                    StandardErrorEncoding  = Encoding.GetEncoding(866)
                };
                if (workingDir != null)
                {
                    startInfo.WorkingDirectory = workingDir;
                }
                process.StartInfo = startInfo;

                OutputWaitHandle = new AutoResetEvent(msecLockTimeout == 0);
                ErrorWaitHandle  = new AutoResetEvent(msecLockTimeout == 0);

                // Capture stdout data
                process.OutputDataReceived += this.OutputDataReceived;

                // Capture stderr data
                process.ErrorDataReceived += this.ErrorDataReceived;

                CurrentCommandLine = AppPath + " " + parameters;


                // Process process's execution end
                process.EnableRaisingEvents = true;
                process.Exited += this.Exited;
                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                SystemProcessObject = process;
            }
            catch (Exception ex)
            {
                process.Dispose();
                throw;
            }

            if (msecLockTimeout != 0)
            {
                if (SystemProcessObject.WaitForExit(msecLockTimeout) &&
                    OutputWaitHandle.WaitOne(msecLockTimeout) &&
                    ErrorWaitHandle.WaitOne(msecLockTimeout))
                {
                    int exitCode = SystemProcessObject.ExitCode;

                    //  Call WaitForExit() once more to ensure stdout and stderr are read to end (see Remarks section in msdn doc for this method)
                    SystemProcessObject.WaitForExit();
                    SystemProcessObject.Dispose();

                    result = new CmdResult
                    {
                        ExitCode       = exitCode,
                        StandardError  = StdError.ToString(),
                        StandardOutput = StdOut.ToString()
                    };
                    StdError.Clear();
                    StdOut.Clear();

                    SystemProcessObject = null;
                }
                else
                {
                    Kill();
                    throw new Exception("Process did not exit in a given timeout");
                }
            }

            return(result);
        }