Exemple #1
0
        private static CmdLine ParseSpacedCmdLine(IList <string> args)
        {
            var cmdLine = new CmdLine();

            for (var i = 0; i < args.Count; i++)
            {
                //Add first as param
                if (args[i].IndexOf('=') != -1)
                {
                    ParseSpaced(args[i], cmdLine);
                }
                else
                {
                    var param = new CmdLineParam {
                        Name = args[i], ShortName = args[i], JoinType = CmdLineJoinType.Space
                    };
                    if (i + 1 < args.Count && !string.IsNullOrEmpty(args[i + 1]))
                    {
                        param.Value = args[i + 1].Trim('"');
                        i++;
                    }
                    else
                    {
                        param.Value    = true;
                        param.JoinType = CmdLineJoinType.OnlyValues;
                    }
                    cmdLine.Add(param);
                }
            }
            return(cmdLine);
        }
        public int Start(CmdLine @params, TimeSpan waitTimeout, bool throwOnBadExitCode)
        {
            var psi = new ProcessStartInfo {
                ErrorDialog = false, CreateNoWindow = true, Arguments = @params.ToString(), FileName = PathToExe
            };
            int     exitCode;
            Process ps = null;

            try
            {
                _log.DebugFormat("starting process");
                ps = Process.Start(psi);
                if (waitTimeout != TimeSpan.Zero)
                {
                    ps.WaitForExit((int)waitTimeout.TotalMilliseconds);
                }
                else
                {
                    ps.WaitForExit();//W.o. timeout
                }
                //By that point process should be finished. if not - kill
                if (!ps.HasExited)
                {
                    try
                    {
                        //Kill
                        ps.Kill();
                    }
                    catch (Exception e)
                    {
                        _log.Error("kill failed", e);
                        throw;
                    }
                }
                exitCode = ps.ExitCode;
                if (exitCode != 0 && throwOnBadExitCode)
                {
                    throw new Exception("bad exit code");
                }
            }
            catch (Exception e)
            {
                _log.Error("process failed", e);
                throw;
            }
            finally
            {
                try
                {
                    if (ps != null)
                    {
                        ps.Close();
                    }
                }
                catch
                {
                }
            }
            return(exitCode);
        }
Exemple #3
0
        private static CmdLine ParseEqualsCmdLine(IEnumerable <string> args)
        {
            var cmdLine = new CmdLine();

            foreach (var paramEq in args)
            {
                ParseSpaced(paramEq, cmdLine);
            }
            return(cmdLine);
        }
Exemple #4
0
 private static void ParseSpaced(string paramEq, CmdLine cmdLine)
 {
     if (!string.IsNullOrEmpty(paramEq))
     {
         var values = paramEq.Split('=');
         var param  = new CmdLineParam {
             Name = values[0], ShortName = values[0], JoinType = CmdLineJoinType.Equals
         };
         if (values.Length == 2 && !string.IsNullOrEmpty(values[1]))
         {
             param.Value = values[1].Trim('"');
         }
         else
         {
             param.Value    = true;
             param.JoinType = CmdLineJoinType.OnlyValues;
         }
         cmdLine.Add(param);
     }
 }