Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am having the need to have a program that will auto shutdown when the computer is done downloading and or installing a program. So I decided to try to make my own in visual C#.

MY Plan: Well the idea is simple, In my application I want the user to be able to write down all the process's names for each of the programs that is being installed or downloaded using a richtextbox. that way the application can keep track of only the selected processes. Then when the processes from the list are done it will trigger an event that will shutdown the computer.

Example: lets say I have Microsoft word installing itself. The process that it is called is msword.exe(for example). The user will type in a richtextbox the name of that process in the application to track it. after it is done installing the Microsoft word program will exit causing the process to end. When my application acknowledges that the process is done then it will fire the shutdown computer trigger.

Question: So the problem that I am having is pretty much everything except turning the computer off :) I need some help, I don't know how to add the process's to my application so it can track it when you write it down in the richtextbox.

Thank You: I will appreciate any help coming from anyone who posts here.

my operating system is windows vista.
Posted

Hi,

I would suggest a couple of changes. First of all i would not let the user type the name of the process to track but would provide him with a list of all the processes on the system. Each entry would have a checkbox. A checked item would be one to track. I'd also add a refresh/ update button to update the list of processes. It's up to you if you'd like to save any processes that are checked but are now missing.

You can get the processes by using
using System.Diagnostics;

internal class ProcessItem
{
	public string Name { get; set; }
	public int Id { get; set; }
}

private readonly BindingList<ProcessItem> m_Processes = new BindingList<ProcessItem>();

public Form1()
{
	InitializeComponent();
	m_CheckedListBox.DataSource = m_Processes;
	m_CheckedListBox.DisplayMember = "Name";
}

private void OnRefreshClicked(object sender, EventArgs e)
{
	Process[] processlist = Process.GetProcesses();
	m_Processes.Clear();
	foreach(Process theprocess in processlist)
	{
		m_Processes.Add(new ProcessItem{Id = theprocess.Id, Name = theprocess.ProcessName});
	}
}

private void OnGetCheckedClicked(object sender, EventArgs e)
{
	foreach (var item in m_CheckedListBox.CheckedItems)
	{
		var processitem = item as ProcessItem;
		if (null != processitem)
		{
			Console.WriteLine("Process {0} ({1}) has been selected", processitem.Name, processitem.Id);
		}
	}
}


Naturally the m_CheckedListBox is of type CheckedListBox which you can easily drop from the toolbox.
The BindingList<T> makes sure that the checkedListBox is updated when the collection changes so; any change to the collection will update the view.

To know when to co me in to action and close he pc is easy; just get the list of processes so once and a while (using e.g. A Timer) see if the checked processes are missing and do your thing.
While your busy you might consider giving the user the option to wait for one or all the processes to exit.


Hope this helps.

Cheers, AT
 
Share this answer
 
v4
Comments
MR. AngelMendez 14-Dec-11 17:47pm    
thanks for the help, I like your ideas but I have a question. you have the code for a console application but my app is a windows forms so how would I record all the processes in a richtextbox or in any other graphical environment?
Addy Tas 15-Dec-11 3:23am    
Hi,

I've updated to add all the items in a CheckedListBox. From this contol you can use the CheckedItems to get a collection with the checked items.


Guess this should get you started :)
Don't forget to accept an answer if you think the answer is complete enough for the question.

Cheers,AT
MR. AngelMendez 15-Dec-11 15:01pm    
OMG lol thanks a lot, I finally was able to advance forward!!!! :)

I was working on another short project just to get me by but I got stuck there too and I haven't been able to proceed in that one. If you don't mind to take a crack at it here is the link to my question. People have given me some solutions but I just don't understand their ways because it is to advance for my level. Thanks again for the help :)

http://www.codeproject.com/Questions/300211/how-to-select-a-node
MR. AngelMendez 15-Dec-11 16:43pm    
I have another question now actually, I am having trouble doing a for each statement for all of the checked processes in the checkedlistbox. for example foreach(checkedlistbox1.selecteditems after that I am stuck. this code is to check all of the checked processes in the list and keep track of them for activity.
Addy Tas 18-Dec-11 14:44pm    
Hi,

If you havn't figured it out by now this should really help you out; i've updated the answer again. Hope it'll make you happy.

Cheers, AT
using System.Diagnostics;

//some of your other code

private void somefunction()//any functioname that you will call after checking your processes and doing all what you want
{
    Process.Start("shutdown", "/s");
}



It is similar to using the below statement in C++:

system("shutdown /s");
 
Share this answer
 
v2
hi

foreach(Process theprocess in processlist)
{
Console.WriteLine(“Process name: {0}; Process ID: {1}”, theprocess.ProcessName, theprocess.Id);
}

from above you can get the process.. after that store these process on so programbble input such check box or on linkbuttons.. then you can code this using delegates...

it will run on your choice..

thanks
sanjeev
 
Share this answer
 
Comments
MR. AngelMendez 15-Dec-11 0:49am    
I still cant seem to get it running here is what I got based on my understanding and everyone's help

public Form1()
{
InitializeComponent();


}
bool run = false;

private void Form1_Load(object sender, EventArgs e)
{
Process[] processlist = Process.GetProcesses();

foreach (Process theprocess in processlist)
{
Console.WriteLine("Process name: {0}; Process ID: {1}", theprocess.ProcessName, theprocess.Id);
if (run == true)
{
richTextBox1.Text.Equals(theprocess);
}
}
}

private void button1_Click(object sender, EventArgs e)
{
run = true;
}
}
}

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