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