Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am working on an application that uses a checkedlistbox that gets all the names of the processes running in the computer and lists them.

My problem is that I want to create a code that will get all of the checked items in the list and do an action.


for example: foreach (checkedlistbox1.checkeditems.....

{
// a code that will check each process that has been checked and monitor them to see if they are still active.
}

and then a code here that will simply shutdown the computer when the selected processes are exited.

here is the entire code that I have so far

C#
public Form1()
        {
            InitializeComponent();
        }

     
      private  int timming = 5;

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_MouseClick(object sender, MouseEventArgs e)
        {

            //process [] means that your creating a list, the list is called
            //"processlist". and = process.getprocesses(); makes processlist
            //collect all the processes and puts them in the list.

            Process[] processlist = Process.GetProcesses();

            //this code means that for each processes in the processlist do an
            //action with the variable "theprocess".
            foreach (Process theprocess in processlist)
            {
                //pretty much adds all the processes in the checkedlistbox
                //theprocess uses processlist's list of processes and displays
                // all the processes by their names.
                checkedListBox1.Items.Add(theprocess.ProcessName);

                timer1 = new System.Windows.Forms.Timer();
                timer1.Tick += new EventHandler(timer1_Tick);
                timer1.Interval = 5000;
                

                ListB.Enabled = false;

                timming--;

                if (timming == 0)
                {
                    timer1.Stop();

                    theprocess.Refresh();

                    timming = 5;

                    timer1.Start();
                
     
                }


I will appreciate everyone's input

thanks,
Posted
Updated 15-Dec-11 16:57pm
v3
Comments
Ganesan Senthilvel 15-Dec-11 22:57pm    
question has been formatted

1 solution

Instead of adding the ProcessName to the checkeditemlist add a class like this

C#
public class CustomCheckedItem<t>
{
    public CustomCheckedItem(string Name, T Value)
    {
        this.Name = Name;
        this.Value = Value;
    }

    public string Name {get;set;}
    public string Value {get;set;}

    public override ToString()
    {
        return Name;
    }
}


You can add the process like this

C#
checkedListBox1.Items.Add(new CustomCheckedItem<Process>(theprocess.ProcessName,theprocess);

Then I would make a read only property in the form that returns the checked items as an IEnumerable. You can then run a foreach on the return IEnumerable.

Something like this

C#
private IEnumerable<process> CheckedItems
{
    get
    {
        return checkedListBox1.CheckedItems.Cast<CustomCheckedCtem<Process>>()
            .Select(r=>r.Value);
    }
}


This would show the process name in the list but have the actuall process object in the background that you could access from the IEnumerable.

Bit of a workaround but should work (so long as you fix the bugs I haven't tried the code)
 
Share this answer
 
v2
Comments
MR. AngelMendez 19-Dec-11 15:19pm    
thanks for your help ill see what I can do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900