public ProcessProperties(ProcessInfo info)
        {
            InitializeComponent();

            m_privateBytesChart = new MonitorChart(chartPrivateBytes, "PrivateBytes");
            m_privateBytesChart.FormatValueEvent = ((value) => { return Util.FormatBytes2((long)value); });
            m_privateBytesChart.LabelFormatAuto = "FormatBytes";
            m_privateBytesChart.LabelFormatZoomed = "RelativeBytes";

            m_handleChart = new MonitorChart(chartHandles, "Handle count");
            m_handleChart.FormatValueEvent = ((value) => { return string.Format("{0}", (long)value); });

            this.Text = "Monitor [ " + info.Name + " ]";
            m_status.Text = "Process: " + info.Name + ", PID: " + info.PID;

            // events
            Closed += OnClosedEvent;

            m_process = ProcessMonitor.CreateProcessMonitor(info.PID);
            m_thread = new Thread(() => MonitoringLoop());
            m_thread.IsBackground = true;
            m_end = false;
            m_thread.Start();
        }
Example #2
0
        /// <summary>
        /// Execute a command (or determine if an input command is valid: set execute:=false), with optional output to console.
        /// </summary>
        /// <param name="input">Input arguments grouped into a string Linked List.</param>
        /// <param name="outputToConsole">If false, does not output text to console.</param>
        /// <param name="execute">If false, does not execute commands (used to determine if input is valid or not).</param>
        /// <returns>true if input was valid (a command could be executed), or false otherwise.</returns>
        private static bool executeCommand(LinkedList <string> input, bool outputToConsole = true, bool execute = true)
        {
            //bool validInput = true;
            switch (input.First.Value)
            {
            case "help":
                if (outputToConsole)
                {
                    Console.WriteLine("[required arg] (optional arg)" + Environment.NewLine
                                      + "Interval format is hh:mm:ss(.xxx)" + Environment.NewLine
                                      + "   ls (sort by pid = false) (use ignore list = true)\tList all running processes. Sorts by name and ignores those on the ignore list by default." + Environment.NewLine
                                      + "   ig\tList all processes in the ignore list." + Environment.NewLine
                                      + "   ig add [name]\tAdd a process name to the ignore list (won't list with ls)." + Environment.NewLine
                                      + "   ig rm [name]\tRemove a process from the ignore list." + Environment.NewLine
                                      + "   killall\tTerminate all running processes on the kill list." + Environment.NewLine
                                      + "   kill [name]\tTerminate a named process, if running." + Environment.NewLine
                                      + "   killpid [pid]\tTerminate a process with specified id, if running." + Environment.NewLine
                                      + "   ki (true/false)\tList all processes in the kill list. true = list only running processes; false = list only non-running processes." + Environment.NewLine
                                      + "   ki add [name]\tAdd a process name to the kill list." + Environment.NewLine
                                      + "   ki rm [name]\tRemove a process from the kill list." + Environment.NewLine
                                      + "   monitor [interval] [repetitions] [console output (true/false)] [command with or without args]\tExecute a command continuously every interval for repetitions (inf for continuous), e.g. 'monitor 00:01:00 inf true killall'" + Environment.NewLine
                                      + "   monitors\tList all running monitors." + Environment.NewLine
                                      + "   monitors [id #] start\tStart a monitor with specified id #. " + Environment.NewLine
                                      + "   monitors [id #] stop\tStop a monitor with specified id #." + Environment.NewLine
                                      + "   monitors [id #] toggle\tToggle a monitor's state (starts if stopped, and stops if running)." + Environment.NewLine
                                      + "   monitors [id #] interval [new interval]\tChange a monitor's interval to a new interval." + Environment.NewLine
                                      + "   monitors [id #] repetitions [new repetitions]\tChange a monitor's repetitions remaining to a new amount.\t" + Environment.NewLine
                                      + "   monitors [id #] rm\tPermanently remove a monitor, and stops it if running." + Environment.NewLine
                                      + "   monitors startall\tStarts all stopped monitors." + Environment.NewLine
                                      + "   monitors stopall\tStops all running monitors." + Environment.NewLine
                                      + "   settings\tList the location on disk where settings are stored." + Environment.NewLine
                                      + "   startup (true/false)\tWith no arguments, lists whether it is true that this program will launch on startup. Args set the value.");
                }
                break;

            case "q":
                if (execute)
                {
                    quit();
                }
                break;

            case "stop":
                //tempMonitorEvent.stop();
                break;

            case "ls":
                bool sortByPID     = false;
                bool useIgnoreList = true;
                if (input.First.Next != null)
                {
                    try
                    {
                        sortByPID = bool.Parse(input.First.Next.Value);
                        //If 1st optional arg specified (sortByPID), 2nd must be specified (useIgnoreList) as well:
                        useIgnoreList = bool.Parse(input.First.Next.Next.Value);
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }
                if (execute && outputToConsole)
                {
                    ProcessMonitor.listProcesses(sortByPID, useIgnoreList, settings.getSetting(header_ignore));
                }
                break;

            case "ig":
                if (input.First.Next == null)     //mode 1: show ignore list
                {
                    if (outputToConsole)
                    {
                        settings.displaySetting(header_ignore, header_ignore_name);
                    }
                }
                else
                {
                    switch (input.First.Next.Value)
                    {
                    case "add":         //mode 2: add to ignore list
                        if (input.First.Next.Next == null || settings.addToSetting(header_ignore, input.First.Next.Next.Value, header_ignore_name, outputToConsole, execute) < 0)
                        {
                            return(false);
                        }
                        break;

                    case "rm":         //mode 3: remove from ignore list
                        if (input.First.Next.Next == null || settings.removeFromSetting(header_ignore, input.First.Next.Next.Value, header_ignore_name, outputToConsole, execute) < 0)
                        {
                            return(false);
                        }
                        break;

                    default:
                        return(false);
                    }
                }
                break;

            case "killall":
                if (input.First.Next != null)
                {
                    return(false);
                }
                if (execute)
                {
                    ProcessMonitor.killall(settings.getSetting(header_kill), outputToConsole);
                }
                break;

            case "kill":
                if (input.First.Next == null)
                {
                    return(false);
                }
                if (execute)
                {
                    ProcessMonitor.killProcess(input.First.Next.Value, outputToConsole);
                }
                break;

            case "killpid":
                if (input.First.Next == null)
                {
                    return(false);
                }
                int pid = stringToInt(input.First.Next.Value);
                if (pid == -1)
                {
                    return(false);
                }
                if (execute)
                {
                    ProcessMonitor.killProcess(pid, outputToConsole);
                }
                break;

            case "ki":
                if (input.First.Next == null)     //mode 1: Display kill list
                {
                    if (outputToConsole)
                    {
                        settings.displaySetting(header_kill, header_kill_name);
                    }
                }
                else
                {
                    switch (input.First.Next.Value)
                    {
                    case "true":         //mode 2: List all processes on kill list that are currently running
                        if (outputToConsole)
                        {
                            settings.displaySetting(header_kill, header_kill_name + " (running)", true, true);
                        }
                        break;

                    case "false":         //mode 3: List all processes on kill list that are currently non-running.
                        if (outputToConsole)
                        {
                            settings.displaySetting(header_kill, header_kill_name + " (non-running)", true, false);
                        }
                        break;

                    case "add":         //mode 4: Add process to kill list
                        if (input.First.Next.Next == null || settings.addToSetting(header_kill, input.First.Next.Next.Value, header_kill_name, outputToConsole, execute) < 0)
                        {
                            return(false);
                        }
                        break;

                    case "rm":         //mode 5: Remove process from kill list
                        if (input.First.Next.Next == null || settings.removeFromSetting(header_kill, input.First.Next.Next.Value, header_kill_name, outputToConsole, execute) < 0)
                        {
                            return(false);
                        }
                        break;

                    default:
                        return(false);
                    }
                }
                break;

            case "monitor":     //usage: monitor [interval] [repetitions] [console output] [command with or without args]
                if (input.First.Next == null || input.First.Next.Next == null || input.First.Next.Next.Next == null)
                {
                    return(false);
                }
                //Parse first 3 arguments
                long   arg_interval;
                double arg_repetitions;
                bool   arg_consoleOutput;
                try
                {
                    arg_interval      = parseInterval(input.First.Next.Value); //attempt to parse interval in format hh:mm:ss(.xxx)
                    arg_consoleOutput = bool.Parse(input.First.Next.Next.Next.Value);
                    if (input.First.Next.Next.Value == "inf")
                    {
                        arg_repetitions = double.PositiveInfinity;
                    }
                    else
                    {
                        int reps = int.Parse(input.First.Next.Next.Value);
                        if (reps < 0)
                        {
                            return(false);
                        }
                        arg_repetitions = (double)reps;
                    }
                }
                catch (Exception e)
                {
                    return(false);
                }
                //Check validity of command argument (with or w/o args):
                input.RemoveFirst();     //remove "monitor"
                string monitorEventCommand = argsToString(input, argSeparator);
                input.RemoveFirst();     //remove [interval]
                input.RemoveFirst();     //remove [repetitions]
                input.RemoveFirst();     //remove [console output]
                if (!isInputValid(input))
                {
                    return(false);
                }
                //Make sure not adding a duplicate MonitorEvent.
                int foundID;
                if (MonitorEvent.containsMonitorEvent(monitorEventList, monitorEventCommand, out foundID))
                {
                    if (outputToConsole)
                    {
                        Console.WriteLine("Monitor #" + foundID.ToString() + " with those parameters is already active.");
                    }
                    return(false);
                }
                //6. Create new MonitorEvent for command. Name it with its command as a string.
                if (execute)
                {
                    TimerCallback eventFunction = _executeCommand_outputToConsole;
                    if (!arg_consoleOutput)
                    {
                        eventFunction = _executeCommand_noOutputToConsole;
                    }
                    DateTime     created      = DateTime.Now;
                    MonitorEvent monitorEvent = new MonitorEvent(monitorEventCommand, eventFunction, (object)input, arg_interval,
                                                                 arg_interval, created, arg_repetitions, arg_consoleOutput, true);
                    monitorEventList.AddLast(monitorEvent);
                    if (outputToConsole)
                    {
                        Console.WriteLine("Monitor #" + monitorEvent.id.ToString() + " created successfully on " + created.ToString() + ".");
                    }
                }
                break;

            case "monitors":
                if (input.First.Next == null)
                {
                    if (outputToConsole)
                    {
                        MonitorEvent.displayMonitorEvents(monitorEventList);
                    }
                }
                else if (input.First.Next.Value == "startall")
                {
                    if (execute)
                    {
                        ProcessMonitor.monitorEvent_startall(monitorEventList, outputToConsole);
                    }
                }
                else if (input.First.Next.Value == "stopall")
                {
                    if (execute)
                    {
                        ProcessMonitor.monitorEvent_stopall(monitorEventList, outputToConsole);
                    }
                }
                else
                {
                    try
                    {
                        int          monitorID = int.Parse(input.First.Next.Value);
                        MonitorEvent found     = MonitorEvent.getMonitorEventByID(monitorEventList, monitorID);
                        if (found == null)
                        {
                            if (outputToConsole)
                            {
                                Console.WriteLine("No monitor with id #" + monitorID.ToString() + " was found.");
                            }
                            return(false);
                        }
                        switch (input.First.Next.Next.Value)
                        {
                        case "start":
                            if (!found.running())
                            {
                                if (execute)
                                {
                                    found.start(outputToConsole);
                                }
                            }
                            else
                            {
                                if (outputToConsole)
                                {
                                    Console.WriteLine("Monitor #" + found.id + " '" + found.name + "' is already running.");
                                }
                            }
                            break;

                        case "stop":
                            if (found.running())
                            {
                                if (execute)
                                {
                                    found.stop(outputToConsole);
                                }
                            }
                            else
                            {
                                if (outputToConsole)
                                {
                                    Console.WriteLine("Monitor #" + found.id + " '" + found.name + "' is already stopped.");
                                }
                            }
                            break;

                        case "toggle":
                            if (execute)
                            {
                                found.toggle(outputToConsole);
                            }
                            break;

                        case "interval":
                            long newInterval = parseInterval(input.First.Next.Next.Next.Value);
                            if (execute)
                            {
                                found.changeInterval(newInterval, newInterval, outputToConsole);
                            }
                            break;

                        case "repetitions":
                            double newRepetitions;
                            if (input.First.Next.Next.Next.Value == "inf")
                            {
                                newRepetitions = double.PositiveInfinity;
                            }
                            int repsParsed = int.Parse(input.First.Next.Next.Next.Value);
                            if (repsParsed < 0)
                            {
                                return(false);
                            }
                            newRepetitions = (double)repsParsed;
                            if (execute)
                            {
                                found.changeRepetitions(newRepetitions, outputToConsole);
                            }
                            break;

                        case "rm":
                            if (execute)
                            {
                                ProcessMonitor.monitorEvent_rm(monitorEventList, monitorID, outputToConsole);
                            }
                            break;

                        default:
                            return(false);
                        }
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }
                break;

            case "settings":
                if (outputToConsole)
                {
                    Console.WriteLine(settings.SETTINGS_PATH);
                }
                break;

            case "startup":
                if (outputToConsole)
                {
                    Console.WriteLine("Sorry, launch on startup is currently unimplemented.");
                }

                /*if (input.First.Next == null) //mode 1: Display setting value T/F for launch on startup
                 * {
                 *  Console.WriteLine("Launch this application on startup: " + Environment.NewLine
                 + "   " + settings[2].First.Value);
                 + }
                 + else
                 + {
                 +  switch (input.First.Next.Value)
                 +  {
                 +      case "true":
                 +          if (settings[2].First.Value != "true")
                 +          {
                 +              try
                 +              {
                 +                  RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                 +                  rk.SetValue(APPLICATION_NAME, Environment.CurrentDirectory + "\\" + APPLICATION_NAME, RegistryValueKind.String);
                 +                  settings[2].First.Value = "true";
                 +                  Console.WriteLine("Successfully set this application to launch on startup.");
                 +                  Console.WriteLine("   (Created registry string value in key '" + rk.Name + "' with name '" + APPLICATION_NAME + "')");
                 +              }
                 +              catch (Exception e)
                 +              {
                 +                  Console.WriteLine("FAILURE:" + Environment.NewLine + "   " + e.Message);
                 +              }
                 +          }
                 +          else Console.WriteLine("This application is already set to launch on startup.");
                 +          break;
                 +      case "false":
                 +          if (settings[2].First.Value != "false")
                 +          {
                 +              try
                 +              {
                 +                  RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                 +                  rk.DeleteValue(APPLICATION_NAME, true);
                 +                  settings[2].First.Value = "false";
                 +                  Console.WriteLine("Successfully changed settings to not launch on startup.");
                 +              }
                 +              catch (Exception e)
                 +              {
                 +                  Console.WriteLine("FAILURE:" + Environment.NewLine + "   " + e.Message);
                 +              }
                 +          }
                 +          else Console.WriteLine("This application is already set to not launch on startup.");
                 +          break;
                 +      default: validInput = false; break;
                 +  }
                 + }*/
                break;

            default:
                return(false);
            }
            return(true);
        }