minaaj

minaaj

  • NA
  • 9
  • 33.4k

help with listbox/method/loop

Nov 24 2010 5:56 AM

Hi All,
I'm nearing the completion of a customized taskmanager, but running into a little problem
The following code fills a listbox with processes:
-------------------------------------------------------------------
private void Getprocs()
        {
            procs = Process.GetProcesses();
            if (Convert.ToInt32(label2.Text) != procs.Length)
                {  
                listBox1.Items.Clear();
                for (int i = 0; i < procs.Length; i++)
                {  
                    if (procs[i].MainWindowTitle != "")
                        {
                        listBox1.Items.Add(procs[i].MainWindowTitle);
                        }
                    else
                        {
                        }
                }    
                label2.Text = procs.Length.ToString();
            }
        }
---------------------------------------------------------------------

And then this method is used to kill the selected process:
-----------------------------------------------------------------------
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                procs[listBox1.SelectedIndex].Kill();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
-----------------------------------------------------------------------

When selecting and killing a process I get a "Access Denied" Messagebox..
However...
Excluding the if/else (procs[i].MainWindowTitle != "") code from the Getproc() method, so that is looks like below, kills a selected process just fine !
-------------------------------------------------------------------------
 private void Getprocs()
        {
            procs = Process.GetProcesses();
            if (Convert.ToInt32(label2.Text) != procs.Length)
                {  
                listBox1.Items.Clear();
                for (int i = 0; i < procs.Length; i++)
                {  
                        listBox1.Items.Add(procs[i].MainWindowTitle);
                }    
                label2.Text = procs.Length.ToString();
            }
        }
--------------------------------------------------------------------------

Can anyone please help with this one ?

Thanks a lot !

Answers (2)