1

I am a WPF newbee trying to build my own Windows Task Manager. Right now i have a window with all the active processes running on my machine showing in a ListView.

Now here is my problem: I have a button called End Process. I want to select a process from the ListView and kill it by pressing the End Process button. I just cant figure it out how to do that.

I have this code (C#) assigned to the button:

private void End_Process_Click(object sender, RoutedEventArgs e)
{
    Process[] procs=Process.GetProcessesByName(ProcessesListView.SelectedItem.ToString());    
    foreach (Process p in procs)
    {
        p.Kill();
    }
}
5
  • What's it not doing correctly?
    – Inisheer
    Jan 5, 2013 at 21:10
  • 1
    Your code kills all processes with the same name as the selected item. Just to make sure that you kill the right process use the process ID as the identifier. Jan 5, 2013 at 21:14
  • Tnx for quick respone. In fact it's not doing anything. As if the button not connected to anything. (MessageBox is working fine though)
    – Shonn Lyga
    Jan 5, 2013 at 21:15
  • 2
    Will the debugger hit a break point in the method? Is the process actually killed (check with Task Manager) and your list is just not updating? What do you mean by "MessageBox is working fine though"? I see no MessageBox. What is the ToString value of SelectedItem It may be different than what you are expecting and GetProcessesByName is not finding anything. Jan 5, 2013 at 21:21
  • 1. There is no breaking, the program is running without exceptions. 2) The process is not killed (i am doing it with notepad to be sure). 3) I mentioned the MessageBox to eliminate the possibility that the button is not triggering the correct method, you can ignore it, its not there anymore. 4) The ToString value is probably the problem. When i use "MessageBox" to check it, i see the full path like: System.Diagnostics...... (Explorer). Do you have any advice as to how can i get only Explorer name without the prefix? Or how can i use the "ProcessId"? Tnx a lot...
    – Shonn Lyga
    Jan 5, 2013 at 21:31

1 Answer 1

2

I suspect the items in your list are the actual Process objects. If so, you can do something like this:

private void End_Process_Click(object sender, RoutedEventArgs e)
{
  Process process = (Process)ProcessesListView.SelectedItem;
  process.Kill();
}
1
  • Wow, 10 minutes on StackOverflow solved me a 2 day headache :) Thanks a lot! Works perfectly...
    – Shonn Lyga
    Jan 5, 2013 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.