Beispiel #1
0
        private void LoadChildProcesses()
        {
            childProcesses = new Dictionary <int, List <Process> >();
            ProcessTree tree = new ProcessTree();

            foreach (Process proc in Process.GetProcesses())
            {
                Process parent = tree.FindParent(proc);
                AddToList(childProcesses, parent == null ? 0 : parent.Id, proc);
            }
        }
Beispiel #2
0
        internal DesktopWindow GetAppMainWindow(Process proc)
        {
            List <int> procList = new List <int>();

            _ = new ProcessTree(proc, procList);

            int maxTry = 20;
            List <DesktopWindow> wins = GetOrderedWindowsByPids(procList);

            while (wins.Count != 1 && maxTry > 0)
            {
                Thread.Sleep(1000);
                wins = GetOrderedWindowsByPids(procList);
                maxTry--;
            }

            if (wins.Count > 0)
            {
                return(wins[0]);
            }

            return(null);
        }
Beispiel #3
0
 private static void PrintTree(string tool, ProcessTree processTree)
 {
     Console.WriteLine($"Process '{tool}' ran under the sandbox. The process tree is the following:");
     PrintNode(string.Empty, processTree.Root);
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            // Define cli options.
            SimpleArgParser simpleArgParser = new SimpleArgParser(new CliFlag[] {
                new CliFlag(CliFlag.SilentFlag)
                {
                    HasValue = false, Description = "Silent mode"
                },
                new CliFlag(CliFlag.ProcessNameFlag)
                {
                    HasValue = true, Description = "Measured process name"
                },
                new CliFlag(CliFlag.VerboseFlag)
                {
                    HasValue = false, Description = "Report all subprocesses"
                },
                new CliFlag(CliFlag.HelpFlag)
                {
                    HasValue = false, Description = "Print help"
                }
            });

            if (!simpleArgParser.Parse(args))
            {
                return;
            }

            // Check that we have received the process filename.
            if (args.Length < 1 || simpleArgParser.HasMatched(CliFlag.HelpFlag))
            {
                const string HelpText = "TimeIt.exe [options] command [command options]\n" +
                                        "\nOptions:\n" +
                                        "\t-s Silent mode (stdout and stderr aren't redirected.)\n" +
                                        "\t-v Verbose mode (Print execution time of all processes.)\n" +
                                        "\t-n Measured process name (Report execution time of this process.)\n" +
                                        "\t-h Print help";
                Console.WriteLine(HelpText);
                return;
            }



            // Parse cli options.
            ParsedOptions options = simpleArgParser.GetParsedOptions();


            // Dont create new window and redirect outputs if silent is not defined.
            ProcessStartInfo startInfo = new ProcessStartInfo(options.ProcessFile, options.ProcessArguments)
            {
                CreateNoWindow         = true,
                UseShellExecute        = false,
                RedirectStandardOutput = !options.Silent,
                RedirectStandardError  = !options.Silent
            };

            // Create child process.
            Process rootProcess = new Process()
            {
                StartInfo = startInfo
            };

            // Handle cancelation, kill the child process.
            Console.CancelKeyPress += KillMeasuredProcess;

            // Handle stdout and stderr streams.
            rootProcess.OutputDataReceived += PrintMeasuredProcessStdout;
            rootProcess.ErrorDataReceived  += PrintMeasuredProcessStderr;

            // Start the measured process and begin reading of stdout, stderr.
            try
            {
                rootProcess.Start();
            }
            catch (Exception)
            {
                ColoredPrint($"Failed to start requested process. Process file: ({options.ProcessFile}) | Arguments: ({options.ProcessArguments})", ConsoleColor.Red, error: true);
                return;
            }

            if (!options.Silent)
            {
                rootProcess.BeginOutputReadLine();
                rootProcess.BeginErrorReadLine();
            }

            // Create process tree from root process.
            m_processTree = new ProcessTree(rootProcess);

            // Wait until measured process exits.
            rootProcess.WaitForExit();

            if (!m_processTree.IsValid)
            {
                ColoredPrint("Unable to query child processes.", ConsoleColor.Red, true);
                return;
            }


            // Measure execution time of all processes in the tree.
            m_processTree.MeasureExecutionTimeOfTree();

            if (options.Verbose)
            {
                foreach (SubProcess subProcess in m_processTree)
                {
                    string times = $"{subProcess.Name}\n{subProcess.Times.FormatProcessTimes()}";
                    ColoredPrint(times, ConsoleColor.DarkGreen);
                }
            }
            else if (options.HasMeasuredProcessName)
            {
                // If user defined -n option report that process time.
                if (m_processTree.TryGetMeasuredProcess(options.MeasuredProcessName, out ProcessTimes times))
                {
                    ReportProcessTimes(options, times, options.MeasuredProcessName);
                    return;
                }
                else
                {
                    ColoredPrint($"Unable to find requested process with name '{options.MeasuredProcessName}'", ConsoleColor.Red, true);
                    return;
                }
            }

            ReportProcessTimes(options, m_processTree.GetOverallTreeTime(), "TotalProcessTree");
        }