Exemple #1
0
        /**
         *
         * @return ProcessExecResult_Struct
         */

        public ProcessExecResult_Struct StartAndWait()
        {
            ProcessExecResult_Struct result = new ProcessExecResult_Struct();
            TimeSpan ElapsedTime;

            Process p = new Process();

            p.StartInfo.Arguments        = Arguments;
            p.StartInfo.FileName         = Quoted(System.IO.Path.Combine(Path.Replace('/', '\\'), Program.Replace('/', '\\')));
            p.StartInfo.WorkingDirectory = Quoted(WorkingDirectory.Replace('/', '\\'));
            p.StartInfo.CreateNoWindow   = true;
            p.StartInfo.UseShellExecute  = false;
            //p.StartInfo.WindowStyle       = System.Diagnostics.ProcessWindowStyle.Hidden;

            if (FName_StandardOutput != null)
            {
                p.StartInfo.RedirectStandardOutput = true;
                p.OutputDataReceived += new DataReceivedEventHandler(StdOutputHandler);
                stdout = new StreamWriter(FName_StandardOutput);
            }
            if (FName_StandardError != null)
            {
                p.StartInfo.RedirectStandardError = true;
                p.ErrorDataReceived += new DataReceivedEventHandler(StdErrorHandler);
                stderr = new StreamWriter(FName_StandardError);
            }
            if (FName_StandardInput != null)
            {
                p.StartInfo.RedirectStandardInput = true;
                stdin = new StreamReader(FName_StandardInput);
            }

            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("ErtmsSolutions.WorkingDirectory           = '{0}'", WorkingDirectory);
                Log.DebugFormat("ErtmsSolutions.Path                       = '{0}'", Path);
                Log.DebugFormat("ErtmsSolutions.Program                    = '{0}'", Program);
                Log.DebugFormat("ErtmsSolutions.Arguments                  = '{0}'", Arguments);
                Log.DebugFormat("ErtmsSolutions.StdIn                      = '{0}'", FName_StandardInput);
                Log.DebugFormat("ErtmsSolutions.StdOut                     = '{0}'", FName_StandardOutput);
                Log.DebugFormat("ErtmsSolutions.StdErr                     = '{0}'", FName_StandardError);
                Log.DebugFormat("ErtmsSolutions.Timeout                    = '{0}'", TimeOut);

                Log.DebugFormat("p.StartInfo.Arguments                     = '{0}'", p.StartInfo.Arguments);
                Log.DebugFormat("p.StartInfo.FileName                      = '{0}'", p.StartInfo.FileName);
                Log.DebugFormat("p.StartInfo.WorkingDirectory              = '{0}'", p.StartInfo.WorkingDirectory);
                Log.DebugFormat("p.StartInfo.CreateNoWindow                = '{0}'", p.StartInfo.CreateNoWindow);
                Log.DebugFormat("p.StartInfo.UseShellExecute               = '{0}'", p.StartInfo.UseShellExecute);
                Log.DebugFormat("p.StartInfo.RedirectStandardOutput        = '{0}'", p.StartInfo.RedirectStandardOutput);
                Log.DebugFormat("p.StartInfo.RedirectStandardError         = '{0}'", p.StartInfo.RedirectStandardError);
                Log.DebugFormat("p.StartInfo.RedirectStandardInput         = '{0}'", p.StartInfo.RedirectStandardInput);
            }

            // LockSetForegroundWindow (LSFW_LOCK);
            try
            {
                p.Start();
                result.ExecResult = ProcessExecResult_Enum.OK;
                result.ExitCode   = 0;
                Log.DebugFormat("  Process started.");
            }
            catch (InvalidOperationException e)
            {
                result.ExitCode   = 0;
                result.ExecResult = ProcessExecResult_Enum.Error;
                result.Message    = e.ToString();
            }
            catch (Win32Exception e)
            {
                result.ExitCode   = 0;
                result.ExecResult = ProcessExecResult_Enum.Error;
                result.Message    = e.ToString();
            }

            if (result.ExecResult == ProcessExecResult_Enum.OK)
            {
                if (FName_StandardOutput != null)
                {
                    p.BeginOutputReadLine();
                }
                if (FName_StandardError != null)
                {
                    p.BeginErrorReadLine();
                }

                if (stdin != null)
                {
                    p.StandardInput.WriteLine(stdin.ReadToEnd());
                    p.StandardInput.Close();
                }


                do
                {
                    ElapsedTime = DateTime.Now - p.StartTime;

                    if (p.HasExited)
                    {
                        Log.DebugFormat("  Processs has exited");
                        result.ExecResult     = ProcessExecResult_Enum.OK;
                        result.ExitCode       = p.ExitCode;
                        result.Message        = "terminated";
                        this.processHasExited = true;
                        break;
                    }

                    if (TimeOut.TotalMilliseconds > 0.0)
                    {
                        if (ElapsedTime > TimeOut)
                        {
                            Log.DebugFormat("  Killing process because of timeout {0}.", ElapsedTime);
                            p.Kill();
                            result.Message        = "Time out";
                            result.ExecResult     = ProcessExecResult_Enum.TimeOut;
                            this.processHasExited = true;
                            break;
                        }
                    }
                    Thread.Sleep(20 /*ms*/);
                } while (true);
            }


            if (stdout != null)
            {
                try
                {
                    p.CancelOutputRead();
                }
                catch (Exception)
                {
                }
                stdout.Close();
                //Set to null to verify if the file is opened yet while it is receiving an Output event
                stdout = null;
            }
            if (stderr != null)
            {
                try
                {
                    p.CancelErrorRead();
                }
                catch (Exception)
                {
                }
                stderr.Close();
                //Set to null to verify if the file is opened yet while it is receiving an Error event
                stderr = null;
            }

            // LockSetForegroundWindow (LSFW_UNLOCK);

            Log.DebugFormat("  Process result : Result={0} ('{1}'), ExitCode={2}", result.ExecResult, result.Message,
                            result.ExitCode);

            return(result);
        }
        /**
         *
         * @return ProcessExecResult_Struct
         */
        public ProcessExecResult_Struct StartAndWait()
        {
            ProcessExecResult_Struct result = new ProcessExecResult_Struct();
            TimeSpan ElapsedTime;

            Process p = new Process();

            p.StartInfo.Arguments = Arguments;
            p.StartInfo.FileName = Quoted(System.IO.Path.Combine(Path.Replace('/', '\\'), Program.Replace('/', '\\')));
            p.StartInfo.WorkingDirectory = Quoted(WorkingDirectory.Replace('/', '\\'));
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.UseShellExecute = false;
            //p.StartInfo.WindowStyle       = System.Diagnostics.ProcessWindowStyle.Hidden;

            if (FName_StandardOutput != null)
            {
                p.StartInfo.RedirectStandardOutput = true;
                p.OutputDataReceived += new DataReceivedEventHandler(StdOutputHandler);
                stdout = new StreamWriter(FName_StandardOutput);
            }
            if (FName_StandardError != null)
            {
                p.StartInfo.RedirectStandardError = true;
                p.ErrorDataReceived += new DataReceivedEventHandler(StdErrorHandler);
                stderr = new StreamWriter(FName_StandardError);
            }
            if (FName_StandardInput != null)
            {
                p.StartInfo.RedirectStandardInput = true;
                stdin = new StreamReader(FName_StandardInput);
            }

            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("ErtmsSolutions.WorkingDirectory           = '{0}'", WorkingDirectory);
                Log.DebugFormat("ErtmsSolutions.Path                       = '{0}'", Path);
                Log.DebugFormat("ErtmsSolutions.Program                    = '{0}'", Program);
                Log.DebugFormat("ErtmsSolutions.Arguments                  = '{0}'", Arguments);
                Log.DebugFormat("ErtmsSolutions.StdIn                      = '{0}'", FName_StandardInput);
                Log.DebugFormat("ErtmsSolutions.StdOut                     = '{0}'", FName_StandardOutput);
                Log.DebugFormat("ErtmsSolutions.StdErr                     = '{0}'", FName_StandardError);
                Log.DebugFormat("ErtmsSolutions.Timeout                    = '{0}'", TimeOut);

                Log.DebugFormat("p.StartInfo.Arguments                     = '{0}'", p.StartInfo.Arguments);
                Log.DebugFormat("p.StartInfo.FileName                      = '{0}'", p.StartInfo.FileName);
                Log.DebugFormat("p.StartInfo.WorkingDirectory              = '{0}'", p.StartInfo.WorkingDirectory);
                Log.DebugFormat("p.StartInfo.CreateNoWindow                = '{0}'", p.StartInfo.CreateNoWindow);
                Log.DebugFormat("p.StartInfo.UseShellExecute               = '{0}'", p.StartInfo.UseShellExecute);
                Log.DebugFormat("p.StartInfo.RedirectStandardOutput        = '{0}'", p.StartInfo.RedirectStandardOutput);
                Log.DebugFormat("p.StartInfo.RedirectStandardError         = '{0}'", p.StartInfo.RedirectStandardError);
                Log.DebugFormat("p.StartInfo.RedirectStandardInput         = '{0}'", p.StartInfo.RedirectStandardInput);
            }

            // LockSetForegroundWindow (LSFW_LOCK);
            try
            {
                p.Start();
                result.ExecResult = ProcessExecResult_Enum.OK;
                result.ExitCode = 0;
                Log.DebugFormat("  Process started.");
            }
            catch (InvalidOperationException e)
            {
                result.ExitCode = 0;
                result.ExecResult = ProcessExecResult_Enum.Error;
                result.Message = e.ToString();
            }
            catch (Win32Exception e)
            {
                result.ExitCode = 0;
                result.ExecResult = ProcessExecResult_Enum.Error;
                result.Message = e.ToString();
            }

            if (result.ExecResult == ProcessExecResult_Enum.OK)
            {
                if (FName_StandardOutput != null)
                    p.BeginOutputReadLine();
                if (FName_StandardError != null)
                    p.BeginErrorReadLine();

                if (stdin != null)
                {
                    p.StandardInput.WriteLine(stdin.ReadToEnd());
                    p.StandardInput.Close();
                }

                do
                {
                    ElapsedTime = DateTime.Now - p.StartTime;

                    if (p.HasExited)
                    {
                        Log.DebugFormat("  Processs has exited");
                        result.ExecResult = ProcessExecResult_Enum.OK;
                        result.ExitCode = p.ExitCode;
                        result.Message = "terminated";
                        this.processHasExited = true;
                        break;
                    }

                    if (TimeOut.TotalMilliseconds > 0.0)
                    {
                        if (ElapsedTime > TimeOut)
                        {
                            Log.DebugFormat("  Killing process because of timeout {0}.", ElapsedTime);
                            p.Kill();
                            result.Message = "Time out";
                            result.ExecResult = ProcessExecResult_Enum.TimeOut;
                            this.processHasExited = true;
                            break;
                        }
                    }
                    Thread.Sleep(20 /*ms*/);
                } while (true);
            }

            if (stdout != null)
            {
                try
                {
                    p.CancelOutputRead();
                }
                catch (Exception)
                {
                }
                stdout.Close();
                //Set to null to verify if the file is opened yet while it is receiving an Output event
                stdout = null;
            }
            if (stderr != null)
            {
                try
                {
                    p.CancelErrorRead();
                }
                catch (Exception)
                {
                }
                stderr.Close();
                //Set to null to verify if the file is opened yet while it is receiving an Error event
                stderr = null;
            }

            // LockSetForegroundWindow (LSFW_UNLOCK);

            Log.DebugFormat("  Process result : Result={0} ('{1}'), ExitCode={2}", result.ExecResult, result.Message,
                result.ExitCode);

            return result;
        }