Esempio n. 1
0
        public List <ProcessWorkItem> GetAll()
        {
            List <ProcessWorkItem> items = new List <ProcessWorkItem>();

            Process[] allProcesses = Process.GetProcesses();
            foreach (Process p in allProcesses)
            {
                ProcessWorkItem processWorkItem = new ProcessWorkItem();
                processWorkItem.procces = p;
                long reminder = 0;
                processWorkItem.caption = string.Format("{0} ({1}, {2}kb+{3}kb)", p.ProcessName, p.Id, Math.DivRem(p.WorkingSet64, 1024, out reminder), Math.DivRem(p.VirtualMemorySize64, 1024, out reminder));

                items.Add(processWorkItem);
            }

            return(items);
        }
Esempio n. 2
0
        public void ExecuteCommand(Extension.IEnsoService service, Command command)
        {
            Logging.AddActionLog(string.Format("ProcessesManager: Executing command '{0}' ...", command.Name));

            if (command.Name == "kill" || command.Name == "restart process")
            {
                if (command.parametersOnExecute[0] is ProcessWorkItem)
                {
                    ProcessWorkItem processWorkItem = command.parametersOnExecute[0] as ProcessWorkItem;
                    FinishCommand(command.Name, processWorkItem.procces, service);
                }
                else if (command.parametersOnExecute[0] is StringWorkItem)
                {
                    Process processById = null;
                    try
                    {
                        int id = Convert.ToInt32(command.parametersOnExecute[0].GetValueAsText());
                        processById = Process.GetProcessById(id);
                    }
                    catch { }
                    if (processById != null)
                    {
                        FinishCommand(command.Name, processById, service);
                    }
                    else
                    {
                        Process[] processesByName = Process.GetProcessesByName(command.parametersOnExecute[0].GetValueAsText());
                        if (processesByName.Length == 1)
                        {
                            FinishCommand(command.Name, processesByName[0], service);
                        }
                        //else if (processesByName.Length > 1)
                        //{
                        //    if (MessageBox.Show(string.Format("{0} proccesses have name '{1}'. Kill'em all?", processesByName.Length, command.parametersOnExecute[0].GetValueAsText()), "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        //    {
                        //        //string message = string.Empty;
                        //        foreach (Process p in processesByName)
                        //        {
                        //            p.Kill();
                        //        }
                        //        MessagesHandler.Display( "Killed");
                        //    }
                        //}
                        else if (processesByName.Length == 0 || processesByName.Length > 1)
                        {
                            //if (MessageBox.Show(string.Format("None is found. Search partialy?"), "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                            //{
                            //MessagesHandler.Display( "0 found. Searching partialy...");

                            Process[]      allProcesses = Process.GetProcesses();
                            List <Process> processesThatContainPhraseInName = new List <Process>();
                            foreach (Process processToCheck in allProcesses)
                            {
                                if (-1 != processToCheck.ProcessName.IndexOf(command.parametersOnExecute[0].GetValueAsText(), StringComparison.InvariantCultureIgnoreCase))
                                {
                                    processesThatContainPhraseInName.Add(processToCheck);
                                }
                            }

                            if (processesThatContainPhraseInName.Count == 0)
                            {
                                MessagesHandler.Display("0 found");
                            }
                            else
                            {
                                string resultNames = string.Empty;
                                foreach (Process p in processesThatContainPhraseInName)
                                {
                                    resultNames += string.Format(",\r\n{0} ({1})", p.ProcessName, p.Id);
                                }
                                if (!string.IsNullOrEmpty(resultNames))
                                {
                                    resultNames = resultNames.Remove(0, 3);
                                }
                                if (MessageBox.Show(string.Format("{0}\r\n\r\nfound. Affect all?", resultNames), "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    //string message = string.Empty;
                                    FinishCommand(command.Name, processesThatContainPhraseInName, service);
                                }
                            }

                            //}
                        }
                    }
                }
                else
                {
                    var warperException = new ApplicationException(string.Format("Process manager: Unsupported type '{0}'found as parameter.", command.parametersOnExecute[0].GetType().FullName));
                    Common.Logging.AddExceptionLog(warperException);
                    MessagesHandler.Display("Unsupported type found as parameter.");
                }
            }
            else
            //if (command.Name == "command name" && command.Postfix == "postfix [item] [item2]")
            //{
            //    MessagesHandler.Display( string.Format("Executing {0} ...", command.Name));

            //}
            //else
            //    if (command.Name == "command name" && command.Postfix == "postfix [item] [item2]")
            //    {
            //        MessagesHandler.Display( string.Format("Executing {0} ...", command.Name));

            //    }
            //    else
            {
                throw new ApplicationException(string.Format("ProcessManager: Command not found. Command: {0} {1}", command.Name, command.Postfix));
            }
        }