Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Diagnostics;
- using System.Collections;
- namespace task_manager
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- //Process[] currentProcesses;
- //List<Proc> currentProcessesList;
- Proc selected;
- List<int> respawnList;
- SortedDictionary<int,Proc> currentProcessesDict;
- public MainWindow()
- {
- InitializeComponent();
- //currentProcessesList = new List<Proc>();
- respawnList = new List<int>();
- currentProcessesDict = new SortedDictionary<int, Proc>();
- Master.SelectionChanged += Master_SelectionChanged;
- Master.ItemsSource = currentProcessesDict;
- priorotyBox.ItemsSource = Enum.GetValues(typeof(System.Diagnostics.ProcessPriorityClass));
- respawnCheckbox.Click += Respawn_Checkbox;
- Button_Refresh(this, null);
- }
- private void Button_Refresh(object sender, RoutedEventArgs e)
- {
- //Master.Items.Clear();
- //currentProcessesList.Clear();
- currentProcessesDict.Clear();
- //Master.SetBinding( System.Diagnostics.Process.GetProcesses());
- //Debug.WriteLine(typeof(System.Diagnostics.Process.GetProcesses()));
- foreach (var proc in System.Diagnostics.Process.GetProcesses())
- {
- Proc pr = new Proc();
- pr.name = proc.ProcessName;
- pr.id = proc.Id;
- pr.respawn = false;
- if(respawnList.Contains(pr.id))
- { pr.respawn = true; }
- //currentProcessesList.Add(pr);
- currentProcessesDict.Add(pr.id, pr);
- }
- List<int> toRemove = new List<int>();
- foreach(int procId in respawnList)
- {
- if (!(currentProcessesDict.ContainsKey(procId)))
- { toRemove.Add(procId); }
- }
- foreach (int procId in toRemove)
- { respawnList.Remove(procId); }
- //"{Binding ElementName=Master, Path=SelectedItem, Mode=OneWay, NotifyOnTargetUpdated=True, Converter={StaticResource PonyConverter}}"
- //selected = currentProcessesList[0];
- //refreshDetails();
- }
- private void Master_SelectionChanged(object sender, RoutedEventArgs e)
- {
- selected = ((KeyValuePair<int,Proc>)Master.SelectedItem).Value;
- refreshDetails();
- }
- private void refreshDetails()
- {
- nameLabel.Content = selected.ToString();
- idLabel.Content = selected.id;
- respawnCheckbox.IsChecked = selected.respawn;
- try
- {
- Process procSelected = System.Diagnostics.Process.GetProcessById(selected.id);
- //nameLabel.Content = selected.ProcessName + " id= " + selected.Id;
- priorityLabel.Content = procSelected.PriorityClass;
- //Debug.WriteLine("");
- //Debug.WriteLine(procSelected.ProcessName);
- //foreach (var si in procSelected.StartInfo.Arguments)
- //{
- // Debug.WriteLine(si);
- //}
- //foreach (DictionaryEntry ev in procSelected.StartInfo.EnvironmentVariables)
- //{
- // Debug.WriteLine(ev.Key + ": "+ev.Value);
- //}
- //Debug.WriteLine(procSelected.StartInfo.WorkingDirectory);
- //Debug.WriteLine("verbs");
- //foreach (var item in procSelected.StartInfo.Verbs)
- //{
- // Debug.WriteLine(item);
- //}
- baseLabel.Content = procSelected.BasePriority;
- List<String> threadsList = new List<string>();
- foreach (System.Diagnostics.ProcessThread t in procSelected.Threads)
- { threadsList.Add("Thread id:" + t.Id + " state:" + t.ThreadState + " base priority:" + t.BasePriority + " current priority:"+t.CurrentPriority); }
- threadList.ItemsSource = threadsList;
- List<String> modulesList = new List<string>();
- foreach (System.Diagnostics.ProcessModule m in procSelected.Modules)
- { modulesList.Add("Module name:"+m.ModuleName + " fileName:"+ m.FileName); }
- moduleList.ItemsSource = modulesList;
- //Debug.WriteLine(procSelected.PriorityClass.GetType().ToString());
- }
- catch (Exception ex)
- {
- //MessageBox.Show("Could not read process details for "+selected.ToString() + "", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
- threadList.ItemsSource = new List<string>();
- moduleList.ItemsSource = new List<string>();
- MessageBox.Show("Could not read process details for " + selected.ToString() + "\nReason: " + ex.Message.ToString(), "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- private void Kill_Click(object sender, RoutedEventArgs e)
- {
- Process procSelected;
- String mainModuleFilename;
- try
- {
- procSelected = System.Diagnostics.Process.GetProcessById(selected.id);
- mainModuleFilename = procSelected.MainModule.FileName;
- procSelected.Kill();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Could not kill process " + selected.ToString() + "\nReason: " + ex.Message.ToString(), "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- finally
- { Button_Refresh(this, null); }
- MessageBox.Show("Process " + selected.name + " killed succesfully", "GREAT SUCCESS!", MessageBoxButton.OK, MessageBoxImage.Information);
- if (selected.respawn)
- {
- try
- {
- //procSelected.Start(mainModuleFilename);
- Process.Start(mainModuleFilename);
- }
- catch (Exception ex)
- {
- MessageBox.Show("Could not respawn process " + selected.ToString() + "\nReason: " + ex.Message.ToString(), "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- MessageBox.Show("Process " + selected.name + " respawned succesfully", "GREAT SUCCESS!", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- }
- private void Respawn_Checkbox(object sender, RoutedEventArgs e)
- {
- try
- {
- Process procSelected = System.Diagnostics.Process.GetProcessById(selected.id);
- }
- catch (Exception ex)
- {
- MessageBox.Show("Process " + selected.name + " isn't running or access is denied", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- currentProcessesDict[selected.id].respawn = (bool)respawnCheckbox.IsChecked;
- if ((bool)respawnCheckbox.IsChecked)
- {
- if (!(respawnList.Contains(selected.id)))
- {
- respawnList.Add(selected.id);
- }
- }
- else
- {
- if (respawnList.Contains(selected.id))
- { respawnList.Remove(selected.id); }
- }
- refreshDetails();
- }
- private void Priority_Change_Click(object sender, RoutedEventArgs e)
- {
- System.Diagnostics.ProcessPriorityClass newPriorityClass;
- if(priorotyBox.SelectedValue == null)
- {
- MessageBox.Show("Select new prioroty first", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- Enum.TryParse<System.Diagnostics.ProcessPriorityClass>(priorotyBox.SelectedValue.ToString(),out newPriorityClass);
- try
- {
- Process procSelected = System.Diagnostics.Process.GetProcessById(selected.id);
- if ( procSelected.PriorityClass.Equals(newPriorityClass))
- {
- MessageBox.Show("Selected priority class is same as before", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- procSelected.PriorityClass = newPriorityClass;
- }
- catch (Exception ex)
- {
- MessageBox.Show("Could not change priority" + selected.ToString() + "\nReason: " + ex.Message.ToString(), "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
- public class Proc
- {
- public String name;
- public int id;
- public bool respawn;
- public override string ToString()
- {
- return name;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement