minaaj

minaaj

  • NA
  • 9
  • 33.4k

help with refreshing date inside a method of a simple taskmanager

Nov 16 2010 7:28 AM

Hi All,

I am working on a very simple taskmanager, which lists all running processes (in a listbox) and let's a user kill a process with a button.

When a process is killed, it should "refresh" the list of process and the recently killed process should ofcourse not be there anymore.

The problem:
I'm trying to "refresh" the listbox with listBox.Items.Clear(); after the process has ben killed , but when the getproc() method is called again, the recently killed process is still in the list.

Does anyone have any suggestions ?

Thanks !
The code:
public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();           
        }
        Process[] procs;
        private void getproc()
        {
            procs = Process.GetProcesses();
            {
                listBox.Items.Clear();
                {
                    foreach (Process proc in procs)
                        listBox.Items.Add(proc.ProcessName);
                }
            }
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                procs[listBox.SelectedIndex].Kill();
                getproc();
            }  
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            getproc();
        }
    }
}

Answers (2)